当前位置:网站首页>C ivalueconverter interface usage example
C ivalueconverter interface usage example
2022-07-23 10:48:00 【biyusr】
01
—
Introduce
IValueConverter The interface is in the namespace System.Windows.Data, Interface defined Convert and ConvertBack Two sets of methods :
public interface IValueConverter{object Convert(object value, Type targetType, object parameter, CultureInfo culture);object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);}}
When the data type or format required by the interface control is inconsistent with the data bound in the background , It is often necessary to resort to IValueConverter Interface .
02
—
give an example
Add a table control to the interface datagrid, Binding data structure StudentList,StudentList This is a ObservableCollection<Students> aggregate ;
Students The data model is defined as follows : It rewrites ToString Method , Four attributes are defined , Namely Id,Name ,Age and AgeValidate ,AgeValidate To validate data , I.e. younger than 16 Or greater than 21, The interface font displays red
Python Programming
Share Python Technical articles , Practical cases , Hot news . What you want to know Python All those things are here ...... When your talent can't support your ambition , Then calm down and study !
10 Original content
official account
[AddINotifyPropertyChangedInterface]public class Students{private int id;public int Id{get { return id; }set{id = value;}}public string Name { get; set; }private int age;public int Age{get { return age; }set{age = value;if(age < 16 || age > 21 ){AgeValidate = true;}else{AgeValidate = false;}}}public bool AgeValidate { get; set; }public override string ToString(){StringBuilder report = new StringBuilder();report.AppendLine($"[Id] = [{Id}]");report.AppendLine($"[Name] = [{Name}]");report.AppendLine($"[Age] = [{Age}]");report.AppendLine($"[AgeValidate] = [{AgeValidate}]");return report.ToString();}}
Interface foreground binding : Mainly through this sentence
Foreground="{Binding AgeValidate,Converter={StaticResource ShowColorConverter}}"datagrid The full code is as follows :
<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>
Define resources
<UserControl.Resources><converters:ShowColorConverter x:Key="ShowColorConverter" /></UserControl.Resources>
IValueConverter Interface implementation : Note that the object Type of value Convert to our target type bool The amount
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
—
Run Demo

Project source code
Baidu SkyDrive : link :https://pan.baidu.com/s/1LmBp1XkXIYz01wyrFQIyYQ
Extraction code : Add Xiaobian to get
Technology Group : Add Xiaobian wechat and comment into the group
Xiaobian wechat :mm1552923
official account :dotNet Programming
边栏推荐
猜你喜欢

理解ASP.NET Core - 基于Cookie的身份认证(Authentication)

PMP每日一练 | 考试不迷路-7.22

TZC 1283: 简单排序 —— 堆排序

Clion + mingw64 configure C language development environment visual studio installation

Comprehensive experiment of realizing private network interworking under mGRE environment

构建人工智能产品/业务的两种策略(by Andrew Ng)

Response对象

300 questions, Lecture 6, quadratic form

mysql log理解

kex_ exchange_ Identification: read: connection reset by peer imperfect solution (one)
随机推荐
Leetcode skimming -- bit by bit record 023
牛客刷题篇——剑指offer (第二期)
C# EventHandler观察者模式
MySQL index operation
Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
Exciting metauniverse! Wealth outlet of next generation Internet
300 题 第六讲 二次型
Hololens third perspective development [nanny level tutorial] [stepping on the pit record]
LeetCode刷题--点滴记录022
[warning] recognizing corrupt image/label during yolov5 training: [errno 2]...... it is impossible to complete the training data set. I will take you to solve it quickly
UNITY VFX syntax error: unexpected token ‘#‘ at kernel CSMain
The topic pub instruction of ros2 appears: failed to populate field: 'vector3' object has no attribute 'x:1' error
PyQt5_ Pyqtgraph mouse draws line segments on the line graph
常见神经网络参数量与计算量
云徙科技CTO李楠:技术和技术人,与数字化共同进化
SQLZOO——SELECT from WORLD Tutorial
Response object
Clion + mingw64 configure C language development environment visual studio installation
TZC 1283: 简单排序 —— 堆排序
XSSGAME小游戏(XSS学习)level1-15
