当前位置: 首页 > news >正文

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

  1. 代码经常遇到类型A转换到类型C,但是已经写好类型A转换类型B类型B转换类型C。往往遇到这种情况,通常会重新写过一个转换器来进行满足需求。以下是解决该痛点。
/// <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;
    }
}
        <local:ValueConverterGroup x:Key="StringToVisibilityConvert">
            <local:StringToUpperConvert />
            <local:StringToVisibilityConvert />
        </local:ValueConverterGroup>
  1. 使用MarkupExtension进行封装转换器
    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

	//使用: <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.转换器单例

    //使用:<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;
       }
   }

相关文章:

  • 北京凝思磐石系统6.0.80安装vm warea tools
  • 【鸿蒙5.0】关于使用卓易通的“文件互传”功能时提示单次导出数量限制为30的一种解决思路
  • UdpClient
  • Chat-TTS-UI:文字转语音 - 本地部署方案
  • 【K8s】 K8s Pod如何通过HostAliases字段配置Host
  • Jmeter下载及环境配置
  • LinuX---Shell脚本创建和执行
  • fastapi+angular实现Tcp在线聊天室功能
  • 大白话阐述react和vue高阶组件的概念、优势及应用场景,以及区别,给出简单高阶组件的实现代码
  • Django-ORM-select_related
  • 如何手动使用下载并且运行 QwQ-32B-GGUF
  • 文件上传安全
  • 大数据-spark3.5安装部署之standalone模式
  • 数据结构:栈的应用举例——进制转换
  • ACwing—跳台阶(bfs+dp+递归+记忆化搜索算法)
  • SpringBoot中使用AJ-Captcha实现行为验证码(滑动拼图、点选文字)
  • C语言零基础入门:嵌入式系统开发之旅
  • windows下安装docker desktop并指定安装目录及汉化
  • 系统可观测性(5)OpenTelemetry基础使用
  • Kotlin 继承
  • 黄土是他们的气质:打破宁夏当代油画创作的沉寂
  • 刘元春在《光明日报》撰文:以法治护航民营经济高质量发展
  • 印度证实印巴已同意停火
  • 巴基斯坦关闭全部领空
  • 上财发布“AI+课程体系”,人工智能如何赋能财经教育?
  • 韩德洙成为韩国执政党总统大选候选人