封装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;
       }
   }
相关推荐
她说彩礼65万1 天前
WPF Prism事件聚合器EventAggregator
wpf
源之缘-OFD先行者1 天前
基于WPF的雷达上位机系统开发实践
wpf·上位机·雷达
xcLeigh2 天前
WPF未来展望:紧跟技术发展趋势,探索新的可能性
c#·wpf
FuckPatience2 天前
WPF 转换器集成资源字典
wpf
她说彩礼65万2 天前
WPF程序使用AutoUpdate实现自动更新
wpf
程序猿人大林2 天前
WPF 元素周期表
ui·c#·wpf
她说彩礼65万3 天前
WPF Prism模块加载 1.Appconfig的配置方式
wpf
埃菲尔铁塔_CV算法3 天前
C# WPF 基础知识学习(四)
学习·c#·wpf
xcLeigh3 天前
WPF与其他技术的集成:与 WinForms、WCF 等协同工作
c#·wpf·优化