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

推荐订阅源

B
Blog RSS Feed
L
LangChain Blog
博客园_首页
量子位
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
S
Secure Thoughts
P
Privacy & Cybersecurity Law Blog
H
Help Net Security
T
Threatpost
N
Netflix TechBlog - Medium
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
C
Cisco Blogs
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
InfoQ
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
U
Unit 42
博客园 - 三生石上(FineUI控件)
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CXSECURITY Database RSS Feed - CXSecurity.com
Security Latest
Security Latest
WordPress大学
WordPress大学
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
C
Check Point Blog
TaoSecurity Blog
TaoSecurity Blog
Project Zero
Project Zero
www.infosecurity-magazine.com
www.infosecurity-magazine.com
SecWiki News
SecWiki News
F
Full Disclosure
S
Security @ Cisco Blogs
T
Tor Project blog
V
V2EX
Y
Y Combinator Blog
S
SegmentFault 最新的问题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
GbyAI
GbyAI
B
Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 古道飘零客

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字段的列。