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(' ');
    }
}
相关推荐
Scout-leaf20 小时前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530141 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
牧马人win1 天前
SmartDapper.Repository
.net
mudtools2 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的2 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21883 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi3 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1233 天前
matlab画图工具
开发语言·matlab
dustcell.3 天前
haproxy七层代理
java·开发语言·前端