一种方法是 使用 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. 【Unix网络编程】 chapter5 TCP客户,服务器程序实例

    chapter5 5.1 概述 5.2 TCP回射服务器程序:main函数 int main(int argc, char **argv) { int listenfd,connfd; pid_t c ...

  2. var与dynamic

    var与dynamic 如果你用MVC写过程序,那么你应该知道ViewBag这个用于前后台的数据传递工具,那么你是否对ViewBag的用法感到过疑惑呢? ViewBag.Mode1l=new obje ...

  3. HTML+CSS实现页面

    使用HTML和CSS实现以下页面: 抽屉首页 个人博客首页 小米官网首页 登录注册页面 一.抽屉首页 1.实现目标:https://dig.chouti.com/ 2.代码: HTML: <!- ...

  4. sweetalert插件使用

    内容: 1.插件介绍 2.插件使用 1.插件介绍 SweetAlert是一个JS插件,能够完美替代JS自带的alert弹出框,并且功能强大,设计优美 使用这个很方便,推荐使用这个插件来写alert s ...

  5. 分水岭算法(理论+opencv实现)

    分水岭算法理论 从意思上就知道通过用水来进行分类,学术上说什么基于拓扑结构的形态学...其实就是根据把图像比作一副地貌,然后通过最低点和最高点去分类! 原始的分水岭: 就是上面说的方式,接下来用一幅图 ...

  6. QT中使用自己定的类和Vector出现错误

    关于QT自定义类不能调用问题: 在Main()函数里面一定要定义include“XXX.cpp”include“XXX.h”,具体原因我也不知道为什么这样定义,是在一个贴吧看见的,弄了好久才解决. 第 ...

  7. JS - 函数,Math,number

    函数分为:关键字function 匿名函数 — 没有名字的函数 有名函数 — 有名字的函数 <body> <div></div> <script> // ...

  8. spring-CXF-maven

    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  9. can't load package the specified module could not be found

    can't load package the specified module could not be found 用 Dependency Walker 2.2Dependency Walker ...

  10. MDAC 重新安装

    MDAC 重新安装 c:\windows\inf 下找出mdac.inf 然后点右键->安装