当前位置:网站首页>通过 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 效果图:
边栏推荐
- 干货|总结那些漏洞工具的联动使用
- 现在网上开户安全么?那么网上开户选哪个证券公司?
- 内存溢出和内存泄漏的区别
- Is the compass stock software reliable? Is it safe to trade stocks?
- Excerpt from "misogyny: female disgust in Japan"
- Flask session forged hctf admin
- SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
- FCOS3D label assignment
- Dry goods | summarize the linkage use of those vulnerability tools
- 请问,在使用flink sql sink数据到kafka的时候出现执行成功,但是kafka里面没有数
猜你喜欢
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)
室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
Help tenants
Thread pool reject policy best practices
社会责任·价值共创,中关村网络安全与信息化产业联盟对话网信企业家海泰方圆董事长姜海舟先生
2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
为租客提供帮助
Leecode3. Longest substring without repeated characters
Flink | multi stream conversion
Custom thread pool rejection policy
随机推荐
Attribute keywords aliases, calculated, cardinality, ClientName
Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
Excuse me, why is it that there are no consumption messages in redis and they are all piled up in redis? Cerely is used.
What parameters need to be reconfigured to replace the new radar of ROS robot
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
Battle Atlas: 12 scenarios detailing the requirements for container safety construction
2022-7-7 Leetcode 844.比较含退格的字符串
Transferring files between VMware and host
2022-7-6 Leetcode 977.有序数组的平方
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
What are the principles for distinguishing the security objectives and implementation methods that cloud computing security expansion requires to focus on?
Common response status codes
[untitled]
Mysql怎样控制replace替换的次数?
为租客提供帮助
Cesium 已知一点经纬度和距离求另一个点的经纬度
得物客服热线的演进之路
Move base parameter analysis and experience summary
118. 杨辉三角
XML文件的解析操作