当前位置:网站首页>通过 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 效果图:
边栏推荐
- . Net core about redis pipeline and transactions
- Vmware 与主机之间传输文件
- 2022-7-6 Leetcode 977. Square of ordered array
- 118. 杨辉三角
- 请问,如图,pyhon云函数提示使用了 pymysql模块,这个是怎么回事?
- 2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
- 566. Reshaping the matrix
- The difference between memory overflow and memory leak
- 【堡垒机】云堡垒机和普通堡垒机的区别是什么?
- 2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
猜你喜欢
Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
Help tenants
Vmware 与主机之间传输文件
实现IP地址归属地显示功能、号码归属地查询
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
高等數學---第八章多元函數微分學1
Vmware共享主机的有线网络IP地址
How to check the ram and ROM usage of MCU through Keil
为租客提供帮助
2022-7-6 Leetcode 977. Square of ordered array
随机推荐
属性关键字Aliases,Calculated,Cardinality,ClientName
Clickhouse (03) how to install and deploy Clickhouse
【日常训练--腾讯精选50】231. 2 的幂
Realize the IP address home display function and number home query
Solve the cache breakdown problem
call undefined function openssl_ cipher_ iv_ length
《厌女:日本的女性嫌恶》摘录
Huawei image address
Learning breakout 2 - about effective learning methods
566. 重塑矩阵
2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
请问指南针股票软件可靠吗?交易股票安全吗?
Excerpt from "misogyny: female disgust in Japan"
内存溢出和内存泄漏的区别
Supply chain supply and demand estimation - [time series]
【网络安全】sql注入语法汇总
Is the spare money in your hand better to fry stocks or buy financial products?
FCOS3D label assignment
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
flask session伪造之hctf admin