当前位置:网站首页>通过 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 效果图:
边栏推荐
- 请问,redis没有消费消息,都在redis里堆着是怎么回事?用的是cerely 。
- 内存溢出和内存泄漏的区别
- 带你掌握三层架构(建议收藏)
- PC端页面如何调用QQ进行在线聊天?
- Seven propagation behaviors of transactions
- Common response status codes
- 3D detection: fast visualization of 3D box and point cloud
- 请问,在使用flink sql sink数据到kafka的时候出现执行成功,但是kafka里面没有数
- [network security] SQL injection syntax summary
- Thread pool reject policy best practices
猜你喜欢

118. Yanghui triangle

Redis can only cache? Too out!

566. 重塑矩阵

AI talent cultivation new ideas, this live broadcast has what you care about

The delivery efficiency is increased by 52 times, and the operation efficiency is increased by 10 times. See the compilation of practical cases of financial cloud native technology (with download)

AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?

Thread pool reject policy best practices

高等数学---第八章多元函数微分学1

MySQL error 28 and solution

MySQL error 28 and solution
随机推荐
2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers
js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
3D Detection: 3D Box和点云 快速可视化
Leetcode simple question sharing (20)
【网络安全】sql注入语法汇总
2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
Transferring files between VMware and host
Redis只能做缓存?太out了!
Details of redis core data structure & new features of redis 6
请问,PTS对数据库压测有好方案么?
【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(二)
"New red flag Cup" desktop application creativity competition 2022
Mysql怎样控制replace替换的次数?
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
566. 重塑矩阵
Data refresh of recyclerview
Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天