封装WPF中转换器常用用法封装

  1. 代码经常遇到类型A转换到类型C,但是已经写好类型A转换到类型B、类型B转换类型C。往往遇到这种情况,通常会重新写过一个转换器来进行满足需求。以下是解决该痛点。
csharp 复制代码
/// <summary>
/// 转换器组转换器
/// </summary>
[ContentProperty(nameof(Converters))]
public class ValueConverterGroup : IValueConverter
{
    public List<IValueConverter> Converters { get; set; } = new List<IValueConverter>();
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (this.Converters is IEnumerable<IValueConverter> converters)
        {
            return converters.Aggregate(value, (current, converter) => converter.Convert(current, targetType, parameter, culture));
        }
        return DependencyProperty.UnsetValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (this.Converters is IEnumerable<IValueConverter> converters)
        {
            return converters.Reverse().Aggregate(value, (current, converter) => converter.ConvertBack(current, targetType, parameter, culture));
        }
        return DependencyProperty.UnsetValue;
    }
}
csharp 复制代码
        <local:ValueConverterGroup x:Key="StringToVisibilityConvert">
            <local:StringToUpperConvert />
            <local:StringToVisibilityConvert />
        </local:ValueConverterGroup>
  1. 使用MarkupExtension进行封装转换器
csharp 复制代码
    public abstract class ValueConverterBase : MarkupExtension, IValueConverter
    {
        public abstract object Convert(object value, Type targetType, object parameter, CultureInfo culture);
        public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

使用该类封装StringToUpperConvert

csharp 复制代码
	//使用: <TextBlock Text="{Binding TestText, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, Converter={local:StringToUpperConvert}}" />
    public class StringToUpperConvert : ValueConverterBase
    {
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is not null && value is string)
            {
                return ((string)value).ToUpper();
            }
            return Binding.DoNothing;
        }
    }

3.转换器单例

csharp 复制代码
    //使用:<TextBlock Text="{Binding TestText, Converter={x:Static local:StringToUpperConvert.Instance}}" />
   public class StringToUpperConvert : IValueConverter
   {
       public static StringToUpperConvert Instance { get;set; } = new StringToUpperConvert();

       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
       {
           if (value is not null && value is string)
           {
               return ((string)value).ToUpper();
           }
           return Binding.DoNothing;
       }
       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
       {
           return DependencyProperty.UnsetValue;
       }
   }
相关推荐
大霸王龙11 小时前
系统模块与功能设计框架
人工智能·wpf
明耀1 天前
WPF DataGrid 默认显示行号
wpf
lph19721 天前
wpf的converter
wpf
fyifei05581 天前
WPF学习PropertyChanged
wpf
爱炸薯条的小朋友1 天前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
baivfhpwxf20231 天前
wpf ListBox 去除item 单击样式
wpf
诗仙&李白1 天前
lnnovationHubTool,用prism+WPF编写的MVVM模式的快速上位机软件开发框架平台
wpf·mvvm·prism·上位机软件开发框架平台
程序员小刘1 天前
【HarmonyOS 5】教育开发实践详解以及详细代码案例
华为·wpf·harmonyos
Java Fans2 天前
在WPF项目中集成Python:Python.NET深度实战指南
python·.net·wpf
布伦鸽2 天前
C# WPF 左右布局实现学习笔记(1)
笔记·学习·c#·wpf