一种方法是 使用 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. git 上传本地代码到github 服务器

    git init git add . (所有文件用 点表示) git commit -m "注释语句" git remote add origin https://github.c ...

  2. 基于RDBMS的OLAP的解决方案

    BI项目如何开发: 了解OLAP的分析方法: 对数据进行多维建模分析,也就是自己设计自己的数据方体,之后程序自动生成数据方体 数据方体: 1.自动成的表结构,仅仅有你需要的列 2.生成一条SQL语句( ...

  3. Nature | 光学CNN层替换传统CNN层,超省电

    CNN 计算效率的研究一直备受关注,但由于功率和带宽的严格限制,CNN 仍难以应用在嵌入式系统如移动视觉.自动驾驶中.在斯坦福大学发表在 Nature 旗下 Scientific Reports 的这 ...

  4. MongoDB集群与LBS应用系列(二)--与Hadoop集成

    长期以来,我每开个系列,只有兴趣写一篇,很难持之与恒.为了克服这个长久以来的性格弱点,以及梳理工作半年的积累.最近一个月会写两篇关于Mongo在地理大数据方面的实践和应用,一篇关于推荐系统的初期准备过 ...

  5. tornado-通过判断后台数据限制登陆--简单的

    import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.options # 提供 ...

  6. OpenCL 双调排序 CPU 版

    ▶ 学习了双调排序,参考(https://blog.csdn.net/xbinworld/article/details/76408595) ● 使用 CPU 排序的代码 #include <s ...

  7. linux系统更新rpm包问题 ,报错rhn-check-2.0.2-5.el7.noarch has missing requires of yum-rhn-plugin >= ('0', '1.6.4', '1')

    报错信息: rhn-check-2.0.2-5.el7.noarch has missing requires of yum-rhn-plugin >= ('0', '1.6.4', '1') ...

  8. win2008 server ping不同

    win2008 server ping不同,网络正常. 下图可以解决!!!

  9. AS3 localToGlobal、globalToLocal方法的总结

    (1).localToGlobal (point)   把point看成在(1)内部,计算出该point相当于stage的坐标 (2).globalToLocal (point)   point为全局 ...

  10. postMessage和sendMessage

    参考:http://www.cnblogs.com/giggle/p/5350288.html(浅谈webWorker) http://blog.csdn.net/zha_zi/article/det ...