当前位置:网站首页>通过 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 效果图:
边栏推荐
- SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
- 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
- Flask session forged hctf admin
- js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
- The difference between memory overflow and memory leak
- FCOS3D label assignment
- 3D Detection: 3D Box和点云 快速可视化
- 请问,PTS对数据库压测有好方案么?
- Flink | multi stream conversion
- Deep understanding of array related problems in C language
猜你喜欢

实现IP地址归属地显示功能、号码归属地查询

Details of redis core data structure & new features of redis 6

2022-7-7 Leetcode 844.比较含退格的字符串

Leecode3. Longest substring without repeated characters

Take you to master the three-tier architecture (recommended Collection)

TPG x AIDU | AI leading talent recruitment plan in progress!

使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )

Redis 核心数据结构 & Redis 6 新特性详

2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
随机推荐
Laravel5 call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
118. Yanghui triangle
postgresql array类型,每一项拼接
云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
带你掌握三层架构(建议收藏)
What parameters need to be reconfigured to replace the new radar of ROS robot
Take you to master the three-tier architecture (recommended Collection)
Is it safe to open an account online now? Which securities company should I choose to open an account online?
[high frequency interview questions] difficulty 2.5/5, simple combination of DFS trie template level application questions
648. 单词替换 : 字典树的经典运用
Seven propagation behaviors of transactions
【日常训练--腾讯精选50】231. 2 的幂
Lavarel之环境配置 .env
Interface automation test - solution of data dependency between interfaces
Excuse me, as shown in the figure, the python cloud function prompt uses the pymysql module. What's the matter?
Beginner XML
Environment configuration of lavarel env
ES日志报错赏析-Limit of total fields
[daily training -- Tencent select 50] 231 Power of 2