当前位置:网站首页>通过 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 效果图:
边栏推荐
- PHP中用下划线开头的变量含义
- DID登陆-MetaMask
- Excuse me, I have three partitions in Kafka, and the flinksql task has written the join operation. How can I give the join operation alone
- Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
- Vmware共享主机的有线网络IP地址
- Wired network IP address of VMware shared host
- Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
- 云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
- 高等數學---第八章多元函數微分學1
- Common response status codes
猜你喜欢
566. 重塑矩阵
Redis can only cache? Too out!
2022-7-6 Leetcode 977. Square of ordered array
使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
Take you to master the three-tier architecture (recommended Collection)
2022-7-7 Leetcode 844. Compare strings with backspace
Flask session forged hctf admin
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
Redis 核心数据结构 & Redis 6 新特性详
随机推荐
Es log error appreciation -limit of total fields
Transferring files between VMware and host
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
mysql ”Invalid use of null value“ 解决方法
648. Word replacement: the classic application of dictionary tree
MySQL error 28 and solution
PostgreSQL array type, each splice
docker部署oracle
Vmware共享主机的有线网络IP地址
【网络安全】sql注入语法汇总
requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement
Common response status codes
call undefined function openssl_ cipher_ iv_ length
FC连接数据库,一定要使用自定义域名才能在外面访问吗?
2022-7-7 Leetcode 844. Compare strings with backspace
带你掌握三层架构(建议收藏)
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
高等數學---第八章多元函數微分學1
Excuse me, when using Flink SQL sink data to Kafka, the execution is successful, but there is no number in Kafka
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天