WPF之DataGrid控件根据某列的值设置行的前景色(色
一种方法是 使用 datagrid的LoadingRow事件:
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
Employee model = e.Row.Item as Employee;
if (model!=null)
{
if (model.Age<)
{
e.Row.Foreground = new SolidColorBrush(Colors.Blue);
}
}
}
这种方法的缺点是只有在加载数据或新增数据时才起效果。
第二种方法就是用 行的样式(RowStyle)+转换器:
转换器类:
//定义值转换器
[ValueConversion(typeof(int), typeof(Brush))]
public class IntToBrushConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int reValue = System.Convert.ToInt32(value);
if (reValue == )
{
return new SolidColorBrush(Colors.Red);
}
else
{
return new SolidColorBrush(Colors.Black);
} } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string strValue = value.ToString();
return value;
} }
声明转换器类:
<Window.Resources>
<local:IntToBrushConvert x:Key="IntToBrushConvert"/>
</Window.Resources>
DataGrid的行样式已经绑定转换器:
<DataGrid HeadersVisibility="Column" ItemsSource="{Binding Path=Employees}"
SelectedItem="{Binding Path=SelectedEmployee}" CanUserAddRows="False" IsReadOnly="True"
AutoGenerateColumns="False">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Foreground"
Value="{Binding Path=Flag,Converter={StaticResource ResourceKey=IntToBrushConvert}}"></Setter>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="工号" Binding="{Binding Path=EmployeeNum}" />
<DataGridTextColumn Header="名称" Binding="{Binding Path=EmployeeName}" />
<DataGridTextColumn Header="职位" Binding="{Binding Path=Title}" />
<DataGridTextColumn Header="年龄" Binding="{Binding Path=Age}" />
<DataGridTextColumn Header="状态" Binding="{Binding Path=Status,Converter={StaticResource ResourceKey=IntToStringConvert}}"/>
</DataGrid.Columns>
</DataGrid>
当Age属性大于22时,把Flag属性赋值为1:
private int m_age;
/// <summary>
/// 年龄
/// </summary>
public int Age
{
get { return m_age; }
set
{
if (value != m_age)
{
m_age = value;
if (m_age > 22)
{
Flag = 1;
}
else
{
Flag = 0;
}
OnPropertyChanged("Age");
}
}
}
运行截图:

WPF之DataGrid控件根据某列的值设置行的前景色(色的更多相关文章
- WPF的DataGrid控件从excel里复制数据然后粘贴
WPF的DataGrid控件不能像winform的DataGridView控件一样,支持值的粘贴.WPF的DataGrid控件本质上是跟数据绑定联系在一起,所以需要进行复制粘贴的操作,可以在wpf里用 ...
- WPF 4 DataGrid 控件(进阶篇一)
原文:WPF 4 DataGrid 控件(进阶篇一) 上一篇<WPF 4 DataGrid 控件(自定义样式篇)>中,我们掌握了DataGrid 列表头.行表头.行.单元格相关的 ...
- WPF 4 DataGrid 控件(进阶篇二)
原文:WPF 4 DataGrid 控件(进阶篇二) 上一篇<WPF 4 DataGrid 控件(进阶篇一)>中我们通过DataGridTemplateColumn 类自定义编辑 ...
- WPF 4 DataGrid 控件(基本功能篇)
原文:WPF 4 DataGrid 控件(基本功能篇) 提到DataGrid 不管是网页还是应用程序开发都会频繁使用.通过它我们可以灵活的在行与列间显示各种数据.本篇将详细介绍WPF 4 中 ...
- WPF 4 DataGrid 控件(自定义样式篇)
原文:WPF 4 DataGrid 控件(自定义样式篇) 在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...
- WPF 自定义DataGrid控件样式
内容转自https://www.cnblogs.com/xiaogangqq123/archive/2012/05/07/2487166.html 一.DataGrid基本样式(一) 小刚已经把Dat ...
- WPF中DataGrid控件内Button的Command和CommandParameter的绑定
场景:视频上传功能,上传列表使用DataGrid控件,视频有不同的状态对应不同的操作,DataGrid中最后一列为操作列,里面是Button控件.希望点击Button后执行对应的操作,但是设置Butt ...
- WPF中DataGrid控件的过滤(Filter)性能分析及优化
DataGrid控件是一个列表控件, 可以进行过滤,排序等.本文主要针对DataGrid的过滤功能进行分析, 并提供优化方案. 1)DataGrid的过滤过程: 用户输入过滤条件 ...
- 关于使用MVVM模式在WPF的DataGrid控件中实现ComboBox编辑列
最近在做一个组态软件的项目,有一个需求需要在建立IO设备变量的时候选择变量的类型等. 建立IO变量的界面是一个DataGrid实现的,可以一行一行的新建变量,如下如所示: 这里需要使用带有ComboB ...
随机推荐
- SpringJdbc之queryForXXX大全解读
一.查询单个字段 Object queryForObject(String sql, Object[] args, Class requiredType) 其中Class requiredTy ...
- 2018-2019-2 《网络对抗技术》Exp5 MSF基础应用 Week7-8 20165233
Exp5 MSF基础应用 目录 一.基础问题 二.攻击实例 主动攻击 ms08_067_netapi(成功) ms10_061_spoolss(失败) 针对浏览器的攻击 ms14_064_ole_co ...
- 《GPU高性能编程CUDA实战》附录三 关于book.h
▶ 本书中用到的公用函数放到了头文件book.h中 #ifndef __BOOK_H__ #define __BOOK_H__ #include <stdio.h> #include &l ...
- Procedure-Function mysql
1.基本用法 drop PROCEDURE if EXISTS sp1; -- 如果存在sp1存储过程则删除掉 create PROCEDURE sp1() SELECT 1; --创建最简单的存储过 ...
- Gradle Maven部署,转化
参考:(易百教程)http://www.yiibai.com/gradle/gradle_deployment.html 目录: Gradle部署 Maven转化为Gradle Gradle部署: c ...
- mysql trigger 设置错误ERROR1419
mysql 触发器设置 background: mysql触发器可以在对数据库数据进行变更(插入,修改,删除)之前或之后触发操作. 在设置mysql触发器时提示: ERROR 1419 (HY000) ...
- springMVC学习记录2-使用注解配置
前面说了一下使用xml配置springmvc,下面再说说注解配置.项目如下: 业务很简单,主页和输入用户名和密码进行登陆的页面. 看一下springmvc的配置文件: <?xml version ...
- 快速预热Buffer_Pool缓冲池
在之前的版本里,如果一台高负荷的机器重启后,内存中大量的热数据被清空,此时就会重新从磁盘加载到Buffer_Pool缓冲池里,这样当高峰期间,性能就会变得很差,连接数就会很高. 在MySQL5.6里, ...
- JS中回调函数的使用
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- java多线程实例
import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.concurr ...