当前位置:网站首页>通过 iValueConverter 给datagrid 的背景颜色 动态赋值
通过 iValueConverter 给datagrid 的背景颜色 动态赋值
2022-07-07 12:10:00 【孔雀东南飞-深圳】
1 创建一个类
public class A
{
private string _result;
public string Result
{
get { return _result; }
set { _Title = result; }
}
}2 创建一个 ColorConverter 类 继承自 IValueConverter 类
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//throw new NotImplementedException();
if(value as string == "Success")
{
return "Green";
}
else
{
return "Red";
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}3 在 xaml 文件中写:
<Window.Resources>
<local:ColorConverter x:Key="MyColorConvert"/>
</Window.Resources>4 在 <DataGrid.Columns> 里写:
<DataGridTemplateColumn Width="150" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Result" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<!--列内容-->
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Result}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Background="{Binding Result,Converter={StaticResource MyColorConvert}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>5 在主函数中写:
private List<A> _con;
public List<A> Con
{
get { return _con; }
set { _con = value; }
}6 在 mainwindow 写:
if(Con == null)
{
Con = new List<A>();
Con.Add(new A()
{
Result = "Success";
});
Con.Add(new A()
{
Result = "Fail";
});
dataGrid.ItemsSource = Con;
}7 效果图:
边栏推荐
- 交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
- Parameter keywords final, flags, internal, mapping keywords internal
- 2022-7-7 Leetcode 844. Compare strings with backspace
- C语言数组相关问题深度理解
- How does MySQL control the number of replace?
- Mysql怎样控制replace替换的次数?
- Es log error appreciation -limit of total fields
- How can the PC page call QQ for online chat?
- Realize the IP address home display function and number home query
- 566. 重塑矩阵
猜你喜欢

Transferring files between VMware and host

手把手教会:XML建模
![SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1](/img/10/6de1ee8467b18ae03894a8d5ba95ff.png)
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1

2022-7-7 Leetcode 844. Compare strings with backspace

Realize the IP address home display function and number home query

高等數學---第八章多元函數微分學1

社会责任·价值共创,中关村网络安全与信息化产业联盟对话网信企业家海泰方圆董事长姜海舟先生

Flink | multi stream conversion

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf

高等数学---第八章多元函数微分学1
随机推荐
FCOS3D label assignment
566. 重塑矩阵
Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
Laravel Form-builder使用
Take you to master the three-tier architecture (recommended Collection)
Redis can only cache? Too out!
When FC connects to the database, do you have to use a custom domain name to access it outside?
Deep understanding of array related problems in C language
The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
C语言数组相关问题深度理解
Help tenants
Drawerlayout suppress sideslip display
The meaning of variables starting with underscores in PHP
648. 单词替换 : 字典树的经典运用
Common response status codes
Cesium 已知一点经纬度和距离求另一个点的经纬度
Evolution of customer service hotline of dewu
Build a secure and trusted computing platform based on Kunpeng's native security
室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)