牧月印象 发表于 2023-10-14 16:43:12

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

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

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

在DataGrid的XAML代码中,如果是常规的内容,通过定义列名称即可显示,如下所示。
如果是日期或者字符串的格式处理,可以使用StringFormat的属性处理,如下所示。
<DataGridTemplateColumn MinWidth="100" Header="创建时间">
    <DataGridTemplateColumn.CellTemplate>
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel><DataTemplate>
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources><TextBlock Text="{Binding CreateTime, StringFormat=yyyy-MM-dd hh:mm}" />
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel></DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>而如果需要转义,则需要设置Converter的对象类,我们的代码如下所示。
<DataGridTemplateColumn MinWidth="100" Header="创建时间">
    <DataGridTemplateColumn.CellTemplate>
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel><DataTemplate>
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources><TextBlock Text="{Binding CreateTime, StringFormat=yyyy-MM-dd hh:mm}" />
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel></DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn><DataGridTemplateColumn MinWidth="100" Header="创建时间">
    <DataGridTemplateColumn.CellTemplate>
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel><DataTemplate>
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources><TextBlock Text="{Binding CreateTime, StringFormat=yyyy-MM-dd hh:mm}" />
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel></DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> 
2、Converter转义类的编写和应用

上面的转义处理中,可以对多个属性进行转义,如前景颜色或者Text文本等等。转义类的定义也比较简单,只需要实现IValueConverter接口即可,一般不需要双向的话,实现Convert函数即可,代码如下所示。
/// <summary>
/// 用来转换性别字符串到颜色
/// </summary>
internal class GenderColorConverter : <strong>IValueConverter</strong>
{
    public object <strong>Convert</strong>(object value, Type targetType, object parameter, CultureInfo culture)
    {
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>if (value is null) return "";

<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>if(value.ToString() == "男")
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>{
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>return "green";
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>}
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>else
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>{
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>return "red";
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>}
    }

    public object <strong>ConvertBack</strong>(object value, Type targetType, object parameter, CultureInfo culture)
    {
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>throw new NotImplementedException();
    }
}或者
    public class<strong> NumberToYesNoStrConverter</strong> : IValueConverter
    {
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>{
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>var flag = ConvertToBoolean(value);
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>return flag ? "是" : "否";
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>}

<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>private bool ConvertToBoolean(object value)
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>{
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>if (value == null)
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>    return false;

<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>return value switch
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>{
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>    float f => f != 0.0f,
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>    double d => d != 0.0f,
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>    int i => i != 0,
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>    long l => l != 0.0f,
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>    _ => throw new NotSupportedException($"value must be a number type, got {value?.GetType()}")
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>};
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>}

<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
    }public class<strong> String2VisibilityConverter</strong> : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>return string.IsNullOrEmpty((string)value) ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>return value != null && (Visibility)value == Visibility.Collapsed;
    }
}

public class<strong> String2VisibilityReConverter</strong> : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>return string.IsNullOrEmpty((string)value) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>return value != null && (Visibility)value == Visibility.Collapsed;
    }
}转义类的函数,可以参考很多案例进行收集一些常用的,也可以自己根据需要创建一些转义函数,虽然感觉比较麻烦一些,不过重用性较好。一般可以放在页面或者窗口的视图代码中,如下代码所示。
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>如果考虑整个App重用这些转义类的内容,也可以放在App.xaml,那么所有的转义函数就是全局可用的了,我倾向于这种方式,可以减少每个窗口或者页面的代码,比较简洁。
<DataGridTemplateColumn MinWidth="100" Header="创建时间">
    <DataGridTemplateColumn.CellTemplate>
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel><DataTemplate>
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources><TextBlock Text="{Binding CreateTime, StringFormat=yyyy-MM-dd hh:mm}" />
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel></DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn><Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources><Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>........................    除了DataGrid中使用,我们也可以其他页面展示的地方使用Converter实现我们的转义效果,如下所示是编辑用户详细信息的页面,其中对性别颜色进行强调,对账户是否过期进行转义和颜色强调。

 其中Xaml的界面代码如下所示
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel>另外,我们也可以使用它来控制一些UI元素的显示,如下对于编辑状态下,不显示用户的初始化密码的文本框架,如下所示。
以及对于角色处理的时候,我们创建角色的时候,仅仅显示简单信息,编辑的时候,显示更多的信息,如下界面代码控制。

新建角色的时候,界面如下所示。

编辑角色的时候,我们根据角色显示更多的信息,如下界面所示。

 
 3、不使用Converter类实现内容转义

 有时候,为了一些特殊的处理,我们可能不一定使用Converter来实现内容的转义,我们也可以直接处理,如下DataGrid里面的列处理,可以直接使用 Triggers 进行转义的处理,如下代码所示。
<DataGridTemplateColumn MinWidth="100" Header="创建时间">
    <DataGridTemplateColumn.CellTemplate>
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel><DataTemplate>
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources><TextBlock Text="{Binding CreateTime, StringFormat=yyyy-MM-dd hh:mm}" />
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel></DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn><DataGridTemplateColumn MinWidth="100" Header="创建时间">
    <DataGridTemplateColumn.CellTemplate>
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel><DataTemplate>
<Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources><TextBlock Text="{Binding CreateTime, StringFormat=yyyy-MM-dd hh:mm}" />
<WrapPanel
    Width="350"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <TextBlock Width="100" Text="账号过期" />
    <CheckBox
      HorizontalAlignment="Right"
      VerticalAlignment="Center"
      VerticalContentAlignment="Top"<strong>
      Content</strong>="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToYesNoStrConverter</strong>}}"
      FontWeight="Bold"
      Foreground="{Binding ViewModel.Item.IsExpire, Converter={StaticResource <strong>BooleanToColorConverter</strong>}}"
      IsChecked="{Binding ViewModel.Item.IsExpire, UpdateSourceTrigger=PropertyChanged}"
      IsEnabled="{Binding ViewModel.IsEdit, Mode=OneWay}" />
</WrapPanel></DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn><Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources><Page.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    <helpers:String2VisibilityConverter x:Key="String2VisibilityConverter" />
    <helpers:String2VisibilityReConverter x:Key="String2VisibilityReConverter" />
</Page.Resources>   
这种处理,代码较多一些,而没法重用,但是贵在直接,而且能够一次性控制多个内容的处理。 
 以上就是在WPF应用端开发中使用Converter类实现内容的转义处理的一些应用心得和总结。

来源:https://www.cnblogs.com/wuhuacong/archive/2023/10/14/17763967.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(8)