WPF-数据转换器

一、单值转换器

1.不传参数

转换器 当Value值大于100时返回红色

csharp 复制代码
public class DataConverter : IValueConverter
    {
        /// <summary>
        /// 表示从源到目标数据转换
        /// </summary>
        /// <param name="value">数据源的值</param>
        /// <param name="targetType">目标类型 根据目标类型进行不同转换</param>
        /// <param name="parameter">参数</param>
        /// <param name="culture">本地国际化时使用</param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter,  CultureInfo culture)
        {
            if (int.Parse(value.ToString()) > 100)
                return Brushes.Red;
            return Brushes.Black;
        }
        /// <summary>
        /// 表示从目标到源头的数据转换 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter,  CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

2.传参数Parameter

选则男或女后 后台的绑定值也发生变化

csharp 复制代码
public class GenderConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,  CultureInfo culture)
    {
        //第一次 男标签触发 value=1 p=1
        //第二次 女标签触发 value=2 p=2
        if (value.ToString() == parameter.ToString())
            return true;
        return false;
    }
    //值返回的处理
    public object ConvertBack(object value, Type targetType, object parameter,  CultureInfo culture)
    {
    	return parameter;
    }
}

二、多值转换器

使用MultiBinding绑定多值,转换器使用数组接收

csharp 复制代码
<Window.Resources>
	<local:TextMulti x:Key="TextMulti"/>
</Window.Resources>
<Grid>
    <TextBox x:Name="tex1"/>
    <TextBox x:Name="tex2"/>
    <TextBox x:Name="tex3"/>
    <TextBox x:Name="tex4">
    <TextBox.Text>
    <MultiBinding Converter="{StaticResource TextMulti}">
        <Binding ElementName="tex1" Path="Text" Mode="TwoWay"/>
        <Binding ElementName="tex2" Path="Text" Mode="TwoWay"/>
        <Binding ElementName="tex3" Path="Text" Mode="TwoWay"/>
    </MultiBinding>
    </TextBox.Text>
    </TextBox>
</Grid>
public class TextMulti:IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    	return values[0] + " " + values[1] + " " + values[2];
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
    	return value.ToString().Split(' ');
    }
}
相关推荐
wjs20242 小时前
状态模式(State Pattern)
开发语言
我命由我123452 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list
liulilittle2 小时前
C++ TAP(基于任务的异步编程模式)
服务器·开发语言·网络·c++·分布式·任务·tap
励志要当大牛的小白菜4 小时前
ART配对软件使用
开发语言·c++·qt·算法
唐青枫5 小时前
C#.NET dapper 详解
c#·.net
爱装代码的小瓶子6 小时前
数据结构之队列(C语言)
c语言·开发语言·数据结构
死也不注释7 小时前
【鸡零狗碎记录】
unity·c#
Maybe_ch7 小时前
.NET-键控服务依赖注入
开发语言·c#·.net
超浪的晨7 小时前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发
终焉暴龙王7 小时前
CTFHub web进阶 php Bypass disable_function通关攻略
开发语言·安全·web安全·php