封装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;
       }
   }
相关推荐
周杰伦fans26 分钟前
WPF中的DataContext以及常见的绑定方式
wpf
没有bug.的程序员1 天前
Redis 数据结构全面解析:从底层编码到实战应用
java·数据结构·redis·wpf
somethingGoWay1 天前
wpf 自定义输入ip地址的文本框
wpf
秋月的私语1 天前
Wpf程序屏幕居中问题修复全记录
wpf
我要打打代码1 天前
WPF启动窗体的三种方式
wpf
R瑾安1 天前
mysql集群部署(Mysql Group Replication)
数据库·mysql·wpf
c#上位机1 天前
wpf中资源的使用
c#·wpf
Vae_Mars2 天前
WPF中的静态资源和动态资源
wpf
somethingGoWay2 天前
wpf 只能输入int类型的文本框
wpf
主宰者2 天前
WPF外部打开html文件
前端·html·wpf