前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# IValueConverter接口用法举例

C# IValueConverter接口用法举例

作者头像
用户9127601
发布2022-03-23 08:46:28
5980
发布2022-03-23 08:46:28
举报
文章被收录于专栏:dotNET编程大全

01 介绍

IValueConverter接口位于名称空间System.Windows.Data,接口定义了Convert和ConvertBack两组方法:

代码语言:javascript
复制
    public interface IValueConverter
    {
        object Convert(object value, Type targetType, object parameter, CultureInfo culture);
        object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
    }
}

在界面控件所需数据类型或格式和后台绑定的数据不一致时,往往需要借助IValueConverter接口进行转换。

02 举例

界面添加一个表格控件datagrid,绑定数据结构StudentList,StudentList这是一个ObservableCollection<Students>集合;

Students数据模型定义如下:其中重写了ToString方法,并定义了四个属性,分别是Id,Name ,Age 和AgeValidate ,AgeValidate 用来验证数据,即年龄小于16或者大于21,时界面字体显示红色

界面前台绑定:主要是通过这句

代码语言:javascript
复制
 Foreground="{Binding AgeValidate,Converter={StaticResource ShowColorConverter}}"

datagrid全部的代码如下:

代码语言:javascript
复制
 <DataGrid Name="dgSourceData" AutoGenerateColumns="False" ItemsSource="{Binding StudentList}" 
                  ContextMenu="{Binding menu1}" RowHeaderWidth="30"  SelectedItem ="{Binding SelectedItems}" SelectionMode="Single" SelectionUnit="Cell"
                                cal:Message.Attach="[Event SelectionChanged]=[GridControl_SelectionChanged($source,$eventArgs)];" 
                               CellEditEnding="dgSourceData_BeginningEdit" SelectionChanged="dgSourceData_SelectionChanged" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name"  Binding="{ Binding Path=Name}" MinWidth="68"/>
                <DataGridTemplateColumn  Header="Age"  MinWidth="68" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Age,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                                     Foreground="{Binding AgeValidate,Converter={StaticResource ShowColorConverter}}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Id"  Binding="{ Binding Path=Id}" MinWidth="48"/>
            </DataGrid.Columns>
        </DataGrid>

定义资源

代码语言:javascript
复制
    <UserControl.Resources>
        <converters:ShowColorConverter x:Key="ShowColorConverter" />
    </UserControl.Resources>

IValueConverter接口实现:注意需要将object类型的value转换为我们的目标类型bool量

代码语言:javascript
复制
public class ShowColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var flag = (bool)value;
            if (flag)
            {
                return "Red";
            }
            else
            {
                return "Black";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

03 运行演示

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-01-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 dotNET编程大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 01 介绍
  • 02 举例
  • 03 运行演示
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档