一种方法是 使用 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控件根据某列的值设置行的前景色(色的更多相关文章

  1. WPF的DataGrid控件从excel里复制数据然后粘贴

    WPF的DataGrid控件不能像winform的DataGridView控件一样,支持值的粘贴.WPF的DataGrid控件本质上是跟数据绑定联系在一起,所以需要进行复制粘贴的操作,可以在wpf里用 ...

  2. WPF 4 DataGrid 控件(进阶篇一)

    原文:WPF 4 DataGrid 控件(进阶篇一)      上一篇<WPF 4 DataGrid 控件(自定义样式篇)>中,我们掌握了DataGrid 列表头.行表头.行.单元格相关的 ...

  3. WPF 4 DataGrid 控件(进阶篇二)

    原文:WPF 4 DataGrid 控件(进阶篇二)      上一篇<WPF 4 DataGrid 控件(进阶篇一)>中我们通过DataGridTemplateColumn 类自定义编辑 ...

  4. WPF 4 DataGrid 控件(基本功能篇)

    原文:WPF 4 DataGrid 控件(基本功能篇)      提到DataGrid 不管是网页还是应用程序开发都会频繁使用.通过它我们可以灵活的在行与列间显示各种数据.本篇将详细介绍WPF 4 中 ...

  5. WPF 4 DataGrid 控件(自定义样式篇)

    原文:WPF 4 DataGrid 控件(自定义样式篇)      在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...

  6. WPF 自定义DataGrid控件样式

    内容转自https://www.cnblogs.com/xiaogangqq123/archive/2012/05/07/2487166.html 一.DataGrid基本样式(一) 小刚已经把Dat ...

  7. WPF中DataGrid控件内Button的Command和CommandParameter的绑定

    场景:视频上传功能,上传列表使用DataGrid控件,视频有不同的状态对应不同的操作,DataGrid中最后一列为操作列,里面是Button控件.希望点击Button后执行对应的操作,但是设置Butt ...

  8. WPF中DataGrid控件的过滤(Filter)性能分析及优化

    DataGrid控件是一个列表控件, 可以进行过滤,排序等.本文主要针对DataGrid的过滤功能进行分析, 并提供优化方案. 1)DataGrid的过滤过程:      用户输入过滤条件       ...

  9. 关于使用MVVM模式在WPF的DataGrid控件中实现ComboBox编辑列

    最近在做一个组态软件的项目,有一个需求需要在建立IO设备变量的时候选择变量的类型等. 建立IO变量的界面是一个DataGrid实现的,可以一行一行的新建变量,如下如所示: 这里需要使用带有ComboB ...

随机推荐

  1. java编译器

    编译: .java变成.class        前端编译   Sun javac Eclipse ECJ .class变成机器码      运行期编译 等HostSpot VM c1,c2 .jav ...

  2. Erasure Coding(纠删码)深入分析

    http://blog.sina.com.cn/s/blog_57f61b490102viq9.html 1.前言 Swift升级到2.0大版本后宣称开始支持纠删码,这其实是一个很有意义的特性,主要是 ...

  3. 开发组件:tmpfs

    [Linux]tmpfs简介及增加方式 https://blog.csdn.net/nextaction/article/details/57076924

  4. 属性,类方法@classmethod

    # 属性的初识# class Person:## def __init__(self,name,hight,weight):# self.name = name# self.__hight = hig ...

  5. Kafka 基本原理

    Kafka 基本原理   来源:阿凡卢 , www.cnblogs.com/luxiaoxun/p/5492646.html 简介 Apache Kafka是分布式发布-订阅消息系统.它最初由Link ...

  6. Javascript,获取元素,write方法

    一:Javascript:弱类型脚本语言,是一种动态类型.实现部分动画效果和用户交互等 -- html是骨架(页面结构)  css样式  js是行为 -- 弱类型体现: JS代码可以写在body,he ...

  7. c++官方文档-模版类

    #include <iostream> using namespace std; template<class T> class MyPair { private: T t[] ...

  8. windows脚本测试

    一. C:\Users\smc892h>systeminfo | findstr 物理内存物理内存总量:     12,167 MB可用的物理内存:   2,103 MB 二.截取字段 参考网站 ...

  9. hbase权限管理

    给用户分配对每个表的操作权限,有RWXCA五种,对应READ, WRITE, EXEC, CREATE, ADMIN hbase(main):222:0> help "grant&qu ...

  10. 34. CentOS-6.3安装配置Apache2.2.6

    安装说明 安装环境:CentOS-6.3安装方式:源码编译安装 软件:httpd-2.2.6.tar.gz  | pcre-8.32.tar.gz | apr-1.4.6.tar.gz | apr-u ...