当前位置:网站首页>通过 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 效果图:
边栏推荐
- Common response status codes
- 实现IP地址归属地显示功能、号码归属地查询
- Realize the IP address home display function and number home query
- Laravel5 call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败
- 請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
- 为租客提供帮助
- 566. Reshaping the matrix
- "New red flag Cup" desktop application creativity competition 2022
- 内存溢出和内存泄漏的区别
- Take you to master the three-tier architecture (recommended Collection)
猜你喜欢

Help tenants
![SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1](/img/10/6de1ee8467b18ae03894a8d5ba95ff.png)
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1

2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out

Flask session forged hctf admin

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

XML文件的解析操作

LeetCode简单题分享(20)

为租客提供帮助

566. Reshaping the matrix

Help tenants
随机推荐
最长上升子序列模型 AcWing 1014. 登山
Beginner XML
call undefined function openssl_ cipher_ iv_ length
ES日志报错赏析-Limit of total fields
Help tenants
Is it safe to open an account online now? Which securities company should I choose to open an account online?
Excellent open source system recommendation of ThinkPHP framework
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
Huawei image address
Es log error appreciation -limit of total fields
LeetCode简单题分享(20)
Is the compass stock software reliable? Is it safe to trade stocks?
Seven propagation behaviors of transactions
The meaning of variables starting with underscores in PHP
Leetcode simple question sharing (20)
FC连接数据库,一定要使用自定义域名才能在外面访问吗?
What are the principles for distinguishing the security objectives and implementation methods that cloud computing security expansion requires to focus on?
648. 单词替换 : 字典树的经典运用
Oracle advanced (V) schema solution
请问,在使用flink sql sink数据到kafka的时候出现执行成功,但是kafka里面没有数