封装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;
       }
   }
相关推荐
△曉風殘月〆15 小时前
如何在WPF中捕获窗口外的事件
wpf
爱吃烤鸡翅的酸菜鱼2 天前
Java 事件发布-订阅机制全解析:从原生实现到主流中间件
java·中间件·wpf·事件·发布订阅
武藤一雄3 天前
WPF中ViewModel之间的5种通讯方式
开发语言·前端·microsoft·c#·wpf
CSharp精选营3 天前
都是微软亲儿子,WPF凭啥干不掉WinForm?这3个场景说明白了
c#·wpf·跨平台·winform
baivfhpwxf20233 天前
wpf TextBlock 控件如何根据内容换行?
wpf
亘元有量-流量变现3 天前
鸿蒙、安卓、苹果音频设备技术深度解析与开发实践
android·wpf·harmonyos·亘元有量·积分墙
软泡芙3 天前
【Bug】ReactiveUI WPF绑定中依赖属性不更新的问题分析与解决方案
java·bug·wpf
浪扼飞舟3 天前
WPF输入验证(ValidationRule)
java·javascript·wpf
IOFsmLtzR5 天前
Flink Agents 源码解读 --- (5) --- ActionExecutionOperator
microsoft·flink·wpf
廋到被风吹走6 天前
【AI】Codex 复杂任务拆解:从“一气呵成“到“步步为营“
人工智能·wpf