翼度科技»论坛 编程开发 .net 查看内容

循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(8)

2

主题

2

帖子

6

积分

新手上路

Rank: 1

积分
6
在我们WPF应用端的时候,和WInform开发或者Vue前端开发一样,有时候也需要对内容进行转义处理,如把一些0,1数值转换为具体含义的文本信息,或者把一些布尔变量转换为是否等,都是常见的转换处理,本篇随笔介绍在WPF应用端对内容使用Converter类实现内容的转义处理的操作。
1、使用Converter实现内容转义或者强调颜色

一般在DataGrid中,我们往往都会根据需要对内容或者颜色进行一些转义处理,如下界面所示用户列表信息中,对性别和是否过期的内容进行颜色和内容的转义处理。

在DataGrid的XAML代码中,如果是常规的内容,通过定义列名称即可显示,如下所示。
  1. [/code]如果是日期或者字符串的格式处理,可以使用StringFormat的属性处理,如下所示。
  2. [code]<DataGridTemplateColumn MinWidth="100" Header="创建时间">
  3.     <DataGridTemplateColumn.CellTemplate>
  4. <WrapPanel
  5.     Width="350"
  6.     VerticalAlignment="Center"
  7.     Orientation="Horizontal">
  8.     <TextBlock Width="100" Text="账号过期" />
  9.     <CheckBox
  10.         HorizontalAlignment="Right"
  11.         VerticalAlignment="Center"
  12.         VerticalContentAlignment="Top"<strong>
  13.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  14.         FontWeight="Bold"
  15.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  16.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  17.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  18. </WrapPanel><DataTemplate>
  19. <Page.Resources>
  20.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  21.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  22.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  23. </Page.Resources><TextBlock Text="{Binding CreateTime, StringFormat=yyyy-MM-dd hh:mm}" />
  24. <WrapPanel
  25.     Width="350"
  26.     VerticalAlignment="Center"
  27.     Orientation="Horizontal">
  28.     <TextBlock Width="100" Text="账号过期" />
  29.     <CheckBox
  30.         HorizontalAlignment="Right"
  31.         VerticalAlignment="Center"
  32.         VerticalContentAlignment="Top"<strong>
  33.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  34.         FontWeight="Bold"
  35.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  36.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  37.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  38. </WrapPanel></DataTemplate>
  39.     </DataGridTemplateColumn.CellTemplate>
  40. </DataGridTemplateColumn>
复制代码
而如果需要转义,则需要设置Converter的对象类,我们的代码如下所示。
  1. <DataGridTemplateColumn MinWidth="100" Header="创建时间">
  2.     <DataGridTemplateColumn.CellTemplate>
  3. <WrapPanel
  4.     Width="350"
  5.     VerticalAlignment="Center"
  6.     Orientation="Horizontal">
  7.     <TextBlock Width="100" Text="账号过期" />
  8.     <CheckBox
  9.         HorizontalAlignment="Right"
  10.         VerticalAlignment="Center"
  11.         VerticalContentAlignment="Top"<strong>
  12.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  13.         FontWeight="Bold"
  14.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  15.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  16.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  17. </WrapPanel><DataTemplate>
  18. <Page.Resources>
  19.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  20.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  21.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  22. </Page.Resources><TextBlock Text="{Binding CreateTime, StringFormat=yyyy-MM-dd hh:mm}" />
  23. <WrapPanel
  24.     Width="350"
  25.     VerticalAlignment="Center"
  26.     Orientation="Horizontal">
  27.     <TextBlock Width="100" Text="账号过期" />
  28.     <CheckBox
  29.         HorizontalAlignment="Right"
  30.         VerticalAlignment="Center"
  31.         VerticalContentAlignment="Top"<strong>
  32.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  33.         FontWeight="Bold"
  34.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  35.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  36.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  37. </WrapPanel></DataTemplate>
  38.     </DataGridTemplateColumn.CellTemplate>
  39. </DataGridTemplateColumn><DataGridTemplateColumn MinWidth="100" Header="创建时间">
  40.     <DataGridTemplateColumn.CellTemplate>
  41. <WrapPanel
  42.     Width="350"
  43.     VerticalAlignment="Center"
  44.     Orientation="Horizontal">
  45.     <TextBlock Width="100" Text="账号过期" />
  46.     <CheckBox
  47.         HorizontalAlignment="Right"
  48.         VerticalAlignment="Center"
  49.         VerticalContentAlignment="Top"<strong>
  50.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  51.         FontWeight="Bold"
  52.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  53.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  54.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  55. </WrapPanel><DataTemplate>
  56. <Page.Resources>
  57.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  58.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  59.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  60. </Page.Resources><TextBlock Text="{Binding CreateTime, StringFormat=yyyy-MM-dd hh:mm}" />
  61. <WrapPanel
  62.     Width="350"
  63.     VerticalAlignment="Center"
  64.     Orientation="Horizontal">
  65.     <TextBlock Width="100" Text="账号过期" />
  66.     <CheckBox
  67.         HorizontalAlignment="Right"
  68.         VerticalAlignment="Center"
  69.         VerticalContentAlignment="Top"<strong>
  70.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  71.         FontWeight="Bold"
  72.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  73.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  74.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  75. </WrapPanel></DataTemplate>
  76.     </DataGridTemplateColumn.CellTemplate>
  77. </DataGridTemplateColumn>
复制代码
 
2、Converter转义类的编写和应用

上面的转义处理中,可以对多个属性进行转义,如前景颜色或者Text文本等等。转义类的定义也比较简单,只需要实现IValueConverter接口即可,一般不需要双向的话,实现Convert函数即可,代码如下所示。
  1. /// <summary>
  2. /// 用来转换性别字符串到颜色
  3. /// </summary>
  4. internal class GenderColorConverter : <strong>IValueConverter</strong>
  5. {
  6.     public object <strong>Convert</strong>(object value, Type targetType, object parameter, CultureInfo culture)
  7.     {
  8. <WrapPanel
  9.     Width="350"
  10.     VerticalAlignment="Center"
  11.     Orientation="Horizontal">
  12.     <TextBlock Width="100" Text="账号过期" />
  13.     <CheckBox
  14.         HorizontalAlignment="Right"
  15.         VerticalAlignment="Center"
  16.         VerticalContentAlignment="Top"<strong>
  17.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  18.         FontWeight="Bold"
  19.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  20.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  21.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  22. </WrapPanel>if (value is null) return "";
  23. <WrapPanel
  24.     Width="350"
  25.     VerticalAlignment="Center"
  26.     Orientation="Horizontal">
  27.     <TextBlock Width="100" Text="账号过期" />
  28.     <CheckBox
  29.         HorizontalAlignment="Right"
  30.         VerticalAlignment="Center"
  31.         VerticalContentAlignment="Top"<strong>
  32.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  33.         FontWeight="Bold"
  34.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  35.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  36.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  37. </WrapPanel>if(value.ToString() == "男")
  38. <WrapPanel
  39.     Width="350"
  40.     VerticalAlignment="Center"
  41.     Orientation="Horizontal">
  42.     <TextBlock Width="100" Text="账号过期" />
  43.     <CheckBox
  44.         HorizontalAlignment="Right"
  45.         VerticalAlignment="Center"
  46.         VerticalContentAlignment="Top"<strong>
  47.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  48.         FontWeight="Bold"
  49.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  50.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  51.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  52. </WrapPanel>{
  53. <Page.Resources>
  54.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  55.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  56.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  57. </Page.Resources>return "green";
  58. <WrapPanel
  59.     Width="350"
  60.     VerticalAlignment="Center"
  61.     Orientation="Horizontal">
  62.     <TextBlock Width="100" Text="账号过期" />
  63.     <CheckBox
  64.         HorizontalAlignment="Right"
  65.         VerticalAlignment="Center"
  66.         VerticalContentAlignment="Top"<strong>
  67.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  68.         FontWeight="Bold"
  69.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  70.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  71.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  72. </WrapPanel>}
  73. <WrapPanel
  74.     Width="350"
  75.     VerticalAlignment="Center"
  76.     Orientation="Horizontal">
  77.     <TextBlock Width="100" Text="账号过期" />
  78.     <CheckBox
  79.         HorizontalAlignment="Right"
  80.         VerticalAlignment="Center"
  81.         VerticalContentAlignment="Top"<strong>
  82.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  83.         FontWeight="Bold"
  84.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  85.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  86.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  87. </WrapPanel>else
  88. <WrapPanel
  89.     Width="350"
  90.     VerticalAlignment="Center"
  91.     Orientation="Horizontal">
  92.     <TextBlock Width="100" Text="账号过期" />
  93.     <CheckBox
  94.         HorizontalAlignment="Right"
  95.         VerticalAlignment="Center"
  96.         VerticalContentAlignment="Top"<strong>
  97.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  98.         FontWeight="Bold"
  99.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  100.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  101.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  102. </WrapPanel>{
  103. <Page.Resources>
  104.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  105.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  106.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  107. </Page.Resources>return "red";
  108. <WrapPanel
  109.     Width="350"
  110.     VerticalAlignment="Center"
  111.     Orientation="Horizontal">
  112.     <TextBlock Width="100" Text="账号过期" />
  113.     <CheckBox
  114.         HorizontalAlignment="Right"
  115.         VerticalAlignment="Center"
  116.         VerticalContentAlignment="Top"<strong>
  117.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  118.         FontWeight="Bold"
  119.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  120.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  121.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  122. </WrapPanel>}
  123.     }
  124.     public object <strong>ConvertBack</strong>(object value, Type targetType, object parameter, CultureInfo culture)
  125.     {
  126. <WrapPanel
  127.     Width="350"
  128.     VerticalAlignment="Center"
  129.     Orientation="Horizontal">
  130.     <TextBlock Width="100" Text="账号过期" />
  131.     <CheckBox
  132.         HorizontalAlignment="Right"
  133.         VerticalAlignment="Center"
  134.         VerticalContentAlignment="Top"<strong>
  135.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  136.         FontWeight="Bold"
  137.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  138.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  139.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  140. </WrapPanel>throw new NotImplementedException();
  141.     }
  142. }
复制代码
或者
  1.     public class<strong> NumberToYesNoStrConverter</strong> : IValueConverter
  2.     {
  3. <WrapPanel
  4.     Width="350"
  5.     VerticalAlignment="Center"
  6.     Orientation="Horizontal">
  7.     <TextBlock Width="100" Text="账号过期" />
  8.     <CheckBox
  9.         HorizontalAlignment="Right"
  10.         VerticalAlignment="Center"
  11.         VerticalContentAlignment="Top"<strong>
  12.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  13.         FontWeight="Bold"
  14.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  15.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  16.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  17. </WrapPanel>public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  18. <WrapPanel
  19.     Width="350"
  20.     VerticalAlignment="Center"
  21.     Orientation="Horizontal">
  22.     <TextBlock Width="100" Text="账号过期" />
  23.     <CheckBox
  24.         HorizontalAlignment="Right"
  25.         VerticalAlignment="Center"
  26.         VerticalContentAlignment="Top"<strong>
  27.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  28.         FontWeight="Bold"
  29.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  30.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  31.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  32. </WrapPanel>{
  33. <Page.Resources>
  34.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  35.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  36.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  37. </Page.Resources>var flag = ConvertToBoolean(value);
  38. <Page.Resources>
  39.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  40.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  41.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  42. </Page.Resources>return flag ? "是" : "否";
  43. <WrapPanel
  44.     Width="350"
  45.     VerticalAlignment="Center"
  46.     Orientation="Horizontal">
  47.     <TextBlock Width="100" Text="账号过期" />
  48.     <CheckBox
  49.         HorizontalAlignment="Right"
  50.         VerticalAlignment="Center"
  51.         VerticalContentAlignment="Top"<strong>
  52.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  53.         FontWeight="Bold"
  54.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  55.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  56.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  57. </WrapPanel>}
  58. <WrapPanel
  59.     Width="350"
  60.     VerticalAlignment="Center"
  61.     Orientation="Horizontal">
  62.     <TextBlock Width="100" Text="账号过期" />
  63.     <CheckBox
  64.         HorizontalAlignment="Right"
  65.         VerticalAlignment="Center"
  66.         VerticalContentAlignment="Top"<strong>
  67.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  68.         FontWeight="Bold"
  69.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  70.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  71.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  72. </WrapPanel>private bool ConvertToBoolean(object value)
  73. <WrapPanel
  74.     Width="350"
  75.     VerticalAlignment="Center"
  76.     Orientation="Horizontal">
  77.     <TextBlock Width="100" Text="账号过期" />
  78.     <CheckBox
  79.         HorizontalAlignment="Right"
  80.         VerticalAlignment="Center"
  81.         VerticalContentAlignment="Top"<strong>
  82.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  83.         FontWeight="Bold"
  84.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  85.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  86.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  87. </WrapPanel>{
  88. <Page.Resources>
  89.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  90.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  91.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  92. </Page.Resources>if (value == null)
  93. <Page.Resources>
  94.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  95.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  96.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  97. </Page.Resources>    return false;
  98. <Page.Resources>
  99.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  100.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  101.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  102. </Page.Resources>return value switch
  103. <Page.Resources>
  104.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  105.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  106.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  107. </Page.Resources>{
  108. <Page.Resources>
  109.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  110.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  111.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  112. </Page.Resources>    float f => f != 0.0f,
  113. <Page.Resources>
  114.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  115.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  116.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  117. </Page.Resources>    double d => d != 0.0f,
  118. <Page.Resources>
  119.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  120.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  121.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  122. </Page.Resources>    int i => i != 0,
  123. <Page.Resources>
  124.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  125.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  126.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  127. </Page.Resources>    long l => l != 0.0f,
  128. <Page.Resources>
  129.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  130.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  131.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  132. </Page.Resources>    _ => throw new NotSupportedException($"value must be a number type, got {value?.GetType()}")
  133. <Page.Resources>
  134.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  135.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  136.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  137. </Page.Resources>};
  138. <WrapPanel
  139.     Width="350"
  140.     VerticalAlignment="Center"
  141.     Orientation="Horizontal">
  142.     <TextBlock Width="100" Text="账号过期" />
  143.     <CheckBox
  144.         HorizontalAlignment="Right"
  145.         VerticalAlignment="Center"
  146.         VerticalContentAlignment="Top"<strong>
  147.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  148.         FontWeight="Bold"
  149.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  150.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  151.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  152. </WrapPanel>}
  153. <WrapPanel
  154.     Width="350"
  155.     VerticalAlignment="Center"
  156.     Orientation="Horizontal">
  157.     <TextBlock Width="100" Text="账号过期" />
  158.     <CheckBox
  159.         HorizontalAlignment="Right"
  160.         VerticalAlignment="Center"
  161.         VerticalContentAlignment="Top"<strong>
  162.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  163.         FontWeight="Bold"
  164.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  165.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  166.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  167. </WrapPanel>public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
  168.     }
复制代码
  1. public class<strong> String2VisibilityConverter</strong> : IValueConverter
  2. {
  3.     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  4.     {
  5. <WrapPanel
  6.     Width="350"
  7.     VerticalAlignment="Center"
  8.     Orientation="Horizontal">
  9.     <TextBlock Width="100" Text="账号过期" />
  10.     <CheckBox
  11.         HorizontalAlignment="Right"
  12.         VerticalAlignment="Center"
  13.         VerticalContentAlignment="Top"<strong>
  14.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  15.         FontWeight="Bold"
  16.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  17.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  18.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  19. </WrapPanel>return string.IsNullOrEmpty((string)value) ? Visibility.Collapsed : Visibility.Visible;
  20.     }
  21.     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  22.     {
  23. <WrapPanel
  24.     Width="350"
  25.     VerticalAlignment="Center"
  26.     Orientation="Horizontal">
  27.     <TextBlock Width="100" Text="账号过期" />
  28.     <CheckBox
  29.         HorizontalAlignment="Right"
  30.         VerticalAlignment="Center"
  31.         VerticalContentAlignment="Top"<strong>
  32.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  33.         FontWeight="Bold"
  34.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  35.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  36.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  37. </WrapPanel>return value != null && (Visibility)value == Visibility.Collapsed;
  38.     }
  39. }
  40. public class<strong> String2VisibilityReConverter</strong> : IValueConverter
  41. {
  42.     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  43.     {
  44. <WrapPanel
  45.     Width="350"
  46.     VerticalAlignment="Center"
  47.     Orientation="Horizontal">
  48.     <TextBlock Width="100" Text="账号过期" />
  49.     <CheckBox
  50.         HorizontalAlignment="Right"
  51.         VerticalAlignment="Center"
  52.         VerticalContentAlignment="Top"<strong>
  53.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  54.         FontWeight="Bold"
  55.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  56.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  57.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  58. </WrapPanel>return string.IsNullOrEmpty((string)value) ? Visibility.Visible : Visibility.Collapsed;
  59.     }
  60.     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  61.     {
  62. <WrapPanel
  63.     Width="350"
  64.     VerticalAlignment="Center"
  65.     Orientation="Horizontal">
  66.     <TextBlock Width="100" Text="账号过期" />
  67.     <CheckBox
  68.         HorizontalAlignment="Right"
  69.         VerticalAlignment="Center"
  70.         VerticalContentAlignment="Top"<strong>
  71.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  72.         FontWeight="Bold"
  73.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  74.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  75.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  76. </WrapPanel>return value != null && (Visibility)value == Visibility.Collapsed;
  77.     }
  78. }
复制代码
转义类的函数,可以参考很多案例进行收集一些常用的,也可以自己根据需要创建一些转义函数,虽然感觉比较麻烦一些,不过重用性较好。一般可以放在页面或者窗口的视图代码中,如下代码所示。
  1. <Page.Resources>
  2.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  3.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  4.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  5. </Page.Resources>
复制代码
如果考虑整个App重用这些转义类的内容,也可以放在App.xaml,那么所有的转义函数就是全局可用的了,我倾向于这种方式,可以减少每个窗口或者页面的代码,比较简洁。
  1. <DataGridTemplateColumn MinWidth="100" Header="创建时间">
  2.     <DataGridTemplateColumn.CellTemplate>
  3. <WrapPanel
  4.     Width="350"
  5.     VerticalAlignment="Center"
  6.     Orientation="Horizontal">
  7.     <TextBlock Width="100" Text="账号过期" />
  8.     <CheckBox
  9.         HorizontalAlignment="Right"
  10.         VerticalAlignment="Center"
  11.         VerticalContentAlignment="Top"<strong>
  12.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  13.         FontWeight="Bold"
  14.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  15.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  16.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  17. </WrapPanel><DataTemplate>
  18. <Page.Resources>
  19.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  20.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  21.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  22. </Page.Resources><TextBlock Text="{Binding CreateTime, StringFormat=yyyy-MM-dd hh:mm}" />
  23. <WrapPanel
  24.     Width="350"
  25.     VerticalAlignment="Center"
  26.     Orientation="Horizontal">
  27.     <TextBlock Width="100" Text="账号过期" />
  28.     <CheckBox
  29.         HorizontalAlignment="Right"
  30.         VerticalAlignment="Center"
  31.         VerticalContentAlignment="Top"<strong>
  32.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  33.         FontWeight="Bold"
  34.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  35.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  36.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  37. </WrapPanel></DataTemplate>
  38.     </DataGridTemplateColumn.CellTemplate>
  39. </DataGridTemplateColumn><Page.Resources>
  40.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  41.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  42.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  43. </Page.Resources><Page.Resources>
  44.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  45.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  46.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  47. </Page.Resources>........................   
复制代码
除了DataGrid中使用,我们也可以其他页面展示的地方使用Converter实现我们的转义效果,如下所示是编辑用户详细信息的页面,其中对性别颜色进行强调,对账户是否过期进行转义和颜色强调。

 其中Xaml的界面代码如下所示
  1. <WrapPanel
  2.     Width="350"
  3.     VerticalAlignment="Center"
  4.     Orientation="Horizontal">
  5.     <TextBlock Width="100" Text="账号过期" />
  6.     <CheckBox
  7.         HorizontalAlignment="Right"
  8.         VerticalAlignment="Center"
  9.         VerticalContentAlignment="Top"<strong>
  10.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  11.         FontWeight="Bold"
  12.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  13.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  14.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  15. </WrapPanel>
复制代码
另外,我们也可以使用它来控制一些UI元素的显示,如下对于编辑状态下,不显示用户的初始化密码的文本框架,如下所示。
  1. [/code]以及对于角色处理的时候,我们创建角色的时候,仅仅显示简单信息,编辑的时候,显示更多的信息,如下界面代码控制。
  2. [align=center][/align]
  3. 新建角色的时候,界面如下所示。
  4. [align=center][/align]
  5. 编辑角色的时候,我们根据角色显示更多的信息,如下界面所示。
  6. [align=center][/align]
  7.  
  8. [size=4] 3、不使用Converter类实现内容转义[/size]
  9.  有时候,为了一些特殊的处理,我们可能不一定使用Converter来实现内容的转义,我们也可以直接处理,如下DataGrid里面的列处理,可以直接使用 Triggers 进行转义的处理,如下代码所示。
  10. [code]<DataGridTemplateColumn MinWidth="100" Header="创建时间">
  11.     <DataGridTemplateColumn.CellTemplate>
  12. <WrapPanel
  13.     Width="350"
  14.     VerticalAlignment="Center"
  15.     Orientation="Horizontal">
  16.     <TextBlock Width="100" Text="账号过期" />
  17.     <CheckBox
  18.         HorizontalAlignment="Right"
  19.         VerticalAlignment="Center"
  20.         VerticalContentAlignment="Top"<strong>
  21.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  22.         FontWeight="Bold"
  23.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  24.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  25.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  26. </WrapPanel><DataTemplate>
  27. <Page.Resources>
  28.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  29.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  30.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  31. </Page.Resources><TextBlock Text="{Binding CreateTime, StringFormat=yyyy-MM-dd hh:mm}" />
  32. <WrapPanel
  33.     Width="350"
  34.     VerticalAlignment="Center"
  35.     Orientation="Horizontal">
  36.     <TextBlock Width="100" Text="账号过期" />
  37.     <CheckBox
  38.         HorizontalAlignment="Right"
  39.         VerticalAlignment="Center"
  40.         VerticalContentAlignment="Top"<strong>
  41.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  42.         FontWeight="Bold"
  43.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  44.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  45.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  46. </WrapPanel></DataTemplate>
  47.     </DataGridTemplateColumn.CellTemplate>
  48. </DataGridTemplateColumn><DataGridTemplateColumn MinWidth="100" Header="创建时间">
  49.     <DataGridTemplateColumn.CellTemplate>
  50. <WrapPanel
  51.     Width="350"
  52.     VerticalAlignment="Center"
  53.     Orientation="Horizontal">
  54.     <TextBlock Width="100" Text="账号过期" />
  55.     <CheckBox
  56.         HorizontalAlignment="Right"
  57.         VerticalAlignment="Center"
  58.         VerticalContentAlignment="Top"<strong>
  59.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  60.         FontWeight="Bold"
  61.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  62.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  63.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  64. </WrapPanel><DataTemplate>
  65. <Page.Resources>
  66.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  67.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  68.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  69. </Page.Resources><TextBlock Text="{Binding CreateTime, StringFormat=yyyy-MM-dd hh:mm}" />
  70. <WrapPanel
  71.     Width="350"
  72.     VerticalAlignment="Center"
  73.     Orientation="Horizontal">
  74.     <TextBlock Width="100" Text="账号过期" />
  75.     <CheckBox
  76.         HorizontalAlignment="Right"
  77.         VerticalAlignment="Center"
  78.         VerticalContentAlignment="Top"<strong>
  79.         Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
  80.         FontWeight="Bold"
  81.         Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
  82.         IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
  83.         IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
  84. </WrapPanel></DataTemplate>
  85.     </DataGridTemplateColumn.CellTemplate>
  86. </DataGridTemplateColumn><Page.Resources>
  87.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  88.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  89.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  90. </Page.Resources><Page.Resources>
  91.     <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  92.     <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
  93.     <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
  94. </Page.Resources>   
复制代码

这种处理,代码较多一些,而没法重用,但是贵在直接,而且能够一次性控制多个内容的处理。 
 以上就是在WPF应用端开发中使用Converter类实现内容的转义处理的一些应用心得和总结。

来源:https://www.cnblogs.com/wuhuacong/archive/2023/10/14/17763967.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具