当前位置:网站首页>通过 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 效果图:
边栏推荐
- Cesium 已知一点经纬度和距离求另一个点的经纬度
- LeetCode简单题分享(20)
- "Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
- Xshell connection server changes key login to password login
- Excerpt from "misogyny: female disgust in Japan"
- 2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
- Navicat run SQL file import data incomplete or import failed
- Thread pool reject policy best practices
- Interface automation test - solution of data dependency between interfaces
- SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
猜你喜欢

Selenium库

最长上升子序列模型 AcWing 1014. 登山

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

手把手教会:XML建模

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

为租客提供帮助
![SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1](/img/10/6de1ee8467b18ae03894a8d5ba95ff.png)
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1

得物客服热线的演进之路

【堡垒机】云堡垒机和普通堡垒机的区别是什么?

Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
随机推荐
Regular expression integer positive integer some basic expressions
Environment configuration of lavarel env
Evolution of customer service hotline of dewu
华为镜像地址
2022-7-6 beginner redis (I) download, install and run redis under Linux
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
Dry goods | summarize the linkage use of those vulnerability tools
C语言数组相关问题深度理解
实现IP地址归属地显示功能、号码归属地查询
[1] Basic knowledge of ros2 - summary version of operation commands
requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement
2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
The difference between memory overflow and memory leak
[daily training] 648 Word replacement
带你掌握三层架构(建议收藏)
AI人才培育新思路,这场直播有你关心的
Attribute keywords aliases, calculated, cardinality, ClientName
2022-7-6 Leetcode 977.有序数组的平方
Lavarel之环境配置 .env
TPG x AIDU | AI leading talent recruitment plan in progress!