惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Affairs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
A
About on SuperTechFans
Last Week in AI
Last Week in AI
博客园 - 叶小钗
博客园 - Franky
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
The Hacker News
The Hacker News
C
Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
LangChain Blog
WordPress大学
WordPress大学
美团技术团队
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
S
Securelist
T
Tenable Blog
I
Intezer
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LINUX DO - 热门话题
博客园 - 三生石上(FineUI控件)
Y
Y Combinator Blog
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
C
Cybersecurity and Infrastructure Security Agency CISA
S
Schneier on Security
L
Lohrmann on Cybersecurity
P
Privacy & Cybersecurity Law Blog
月光博客
月光博客
P
Proofpoint News Feed
Vercel News
Vercel News
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
博客园 - 【当耐特】
A
Arctic Wolf
aimingoo的专栏
aimingoo的专栏

博客园 - 古道飘零客

Easyui combogrid添加toolbar SQL导出到Excel 存储过程 [转]系统性能调优吐血总结分享 [转]C#读取CSV,Excel,Txt文件,删除文件,拷贝文件 [转]C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件.... JS四舍五入BUG解决 IIRF(Ionics Isapi Rewrite Filt er)实现在IIS 5/6上重写Url Repeater控件实现左侧快捷菜单 - 古道飘零客 - 博客园 JS和CS互访(非常经典) [转]IList及泛型集合类转换DataTable - 古道飘零客 - 博客园 关于sqlserver2005中的bit数据类型 C#小写转大写 - 古道飘零客 - 博客园 URL、Session、Cookies、Server.Transfer、Application和跨页面传送详解 sql server 2005 安装提示COM+ 和性能监视器计数器要求错误 oracle数据分组排名次 [转cnblogs]网页静态 js如何判断小数点后有几位? - 古道飘零客 - 博客园 怎样读取本地Excel数据,并保存到时服务器 - 古道飘零客 - 博客园 C#项目打包,并自动安装SQL数据库 - 古道飘零客 - 博客园
[WPF]DataGrid行前景色根据单元格编辑值变色
古道飘零客 · 2012-11-30 · via 博客园 - 古道飘零客

解决办法一:这个方法简单一点用后台代码实现

public static DataGridRow GetDataGridRow(DataGrid dataGrid, int rowIndex)

        {

            DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);

            if (row == null)

            {

                dataGrid.UpdateLayout();

                row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);                

            }

            return row;

        }

取行Index ,取值再判断一下你的条件,符合就改
调用
 DataGridRow drow=GetDataGridRow(dataGrid1, rowIndex)
 drow.Foreground = Brushes.Blue;

方法二:代码看起来多点。用数据触发器实现

下面的代码的作用是,当值等于23的时候DataGridRow行的字体颜色为红色。

        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Age}" Value="23">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <ToolTip>
                                <TextBlock Text="{Binding Age,StringFormat='年龄:{0}超出'}"/>
                            </ToolTip>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Foreground" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

  想要操作等于以外的情况时,只能自己写一个值转换类,继承IValueConverter 。

namespace dataGrid_binding.ViewModel
{
    public class AgeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, 
System.Globalization.CultureInfo culture)
        {
            int v = 0, p = 0;
            int.TryParse(value.ToString(), out v);
            int.TryParse(parameter.ToString(), out p);
            return v > p;
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, 
System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

以下的内容写在Xaml页面上。

<Window x:Class="dataGrid_binding.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:VM="clr-namespace:dataGrid_binding.ViewModel" 
        Title="MainWindow" Height="395" Width="713">
    <Window.Resources>
        <VM:StudentViewModel x:Key="ViewModel" />
 
        <VM:AgeConverter x:Key="AgeConvert" />
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Age,Converter={StaticResource AgeConvert},
ConverterParameter=22}" Value="True">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <ToolTip>
                                <TextBlock Text="{Binding Age,StringFormat='年龄:{0}超出'}"/>
                            </ToolTip>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Foreground" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

<DataTrigger Binding="{Binding Age,Converter={StaticResource AgeConvert},ConverterParameter=22}" Value="True">

  上面这句的意思就是Binding了数据Age,转换这个值,对应上面的值转换类,当值大于ConverterParameter传过去的值的时候,做操作。

  这个样式就对应了DataGrid中绑定了Age字段的列。