初学WPF ,  希望对DataGrid 中所属的一个Column名下的值的颜色动态修改

<DataGridTextColumn Header="隐含回购利率(%)" Binding="{Binding ImpRepo, StringFormat={}{0:f4}}" MinWidth="60" IsReadOnly="True" />

但是使用如下语句并没有起作用:

Foreground="{Binding Path=ImpRepo,Converter={StaticResource IRRColorConvert}}"

IRRColorConvert定义在XAML前方:

<UserControl.Resources>

        <ResourceDictionary>         

            <local:IRRColorConvert x:Key="IRRColorConvert"/>

...........

IRRColorConvert代码如下:

 class IRRColorConvert : IValueConverter
{
public object Convert(object value, Type typeTarget, object param, CultureInfo culture)
{
double dValue = (double)value;
byte r, g, b = 0;
if (dValue >= 0)
{
if (dValue >= 5)
r = 254;
else
r = (byte)(int)(norlib.Maths.GetValue(new double[] { 0, 5 }, dValue, new double[] { 200, 255 }));
g = 89;
b = 61;
}
else
{
r = 56;
if (dValue <= -30)
g = 150;
else
g = (byte)(int)(norlib.Maths.GetValue(new double[] { -30, 0 }, dValue, new double[] { 150, 255 }));
b = 61;
}
return new SolidColorBrush(Color.FromRgb(r, g, b));
}
public object ConvertBack(object value, Type typeTarget, object param, CultureInfo culture)
{
return "";
}
}

后来才知道 Foreground 不是 DataGridTextColumn 的依赖项属性

关于这些概念请参看如下链接:

http://www.cnblogs.com/axzxs2001/archive/2010/04/25/1719857.html

http://blog.csdn.net/datoumimi/article/details/8033682

解决的办法如下,使用DataGridTemplateColumn+TextBlock 代替DataGridTextColumn

因为TextBlock的Foreground是Dependency Proeprty

可以比较MSDN:

DataGridTextColumn:

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtextcolumn.foreground(VS.95).aspx

TextBlock:

http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.foreground.aspx

解决代码如下:

<DataGridTemplateColumn Header="隐含回购利率(%)" MinWidth="70" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ImpRepo, StringFormat={}{0:n2},UpdateSourceTrigger=PropertyChanged}" Foreground="{Binding Path=ImpRepo,Converter={StaticResource IRRColorConvert}}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

国外对此问题的一些说明:

http://social.msdn.microsoft.com/Forums/silverlight/en-US/86899afe-855e-422a-b07a-1f50c5f166fd/why-does-datagridtextcolumnforeground-etc-not-support-binding

http://stackoverflow.com/questions/5926849/datagridtextcolumn-how-to-bind-isreadonly

希望可以帮到大家

WPF DataGrid foreground 绑定问题的更多相关文章

  1. (WPF) DataGrid之绑定

    通过ObservableCollection 绑定到 DataGrid. 1. 前台Xaml. <DataGrid x:Name="dgMeasurements" Horiz ...

  2. WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.

    WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次  悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...

  3. WPF DataGrid绑定一个组合列

    WPF DataGrid绑定一个组合列 前台: <Page.Resources>        <local:InfoConverter x:Key="converter& ...

  4. WPF DataGrid 绑定行双击行命令

    WPF DataGrid 绑定行双击行命令 <DataGrid ...> <DataGrid.InputBindings> <MouseBinding MouseActi ...

  5. WPF dataGrid下的ComboBox的绑定

    WPF dataGrid下的ComboBox的绑定 Wpf中dataGrid中的某列是comboBox解决这个问题费了不少时间,不废话了直接上代码 xaml 代码 <DataGridTempla ...

  6. WPF DataGrid 双击行 获得绑定数据

    原文:WPF DataGrid 双击行 获得绑定数据 1)增加事件 2)增加对象获取 1)事件代码 Datagrid 增加事件 MouseDoubleClick="dataGrid_Mous ...

  7. WPF Datagrid 动态生成列 并绑定数据

    原文:WPF Datagrid 动态生成列 并绑定数据 说的是这里 因为列头是动态加载的 (后台for循环 一会能看到代码) 数据来源于左侧列 左侧列数据源 当然num1 属于临时的dome使用  可 ...

  8. WPF DataGrid 绑定数据及时更新的处理

    原文:WPF DataGrid 绑定数据及时更新的处理 默认情况下datagrid 绑定数据源后,在界面编辑某一列后,数据不会及时更新到内存对象中.如在同一行上有一个命令对来获取 当前选中行(内存对象 ...

  9. WPF DATAGrid 空白列 后台绑定列 处理

    原文:WPF DATAGrid 空白列 后台绑定列 处理 AutoGenerateColumns <DataGrid x:Name="dataGrid" Margin=&qu ...

随机推荐

  1. MAC上Nuclide的安装

    MAC上Nuclide的安装 本文版权归作者全部,如需转载请联系孟祥月 CSDN博客:http://blog.csdn.net/mengxiangyue 独立博客:http://mengxiangyu ...

  2. PHP中的多行字符串传递给JavaScript方法两则

    PHP和JavaScript都是初学.近期有这么个需求: 例如说有一个PHP的多行字符串: $a = <<<EOF thy38 csdn blog EOF; 传递给JavaScrip ...

  3. IE浏览器部分版本不支持background-size属性问题

    background-size是CSS3新增的属性,但是IE8以下还是不支持,可以通过滤镜来实现这样的一个效果 background-size:contain; // 缩小图片来适应元素的尺寸(保持像 ...

  4. UVa 12587 Reduce the Maintenance Cost(Tarjan + 二分 + DFS)

    题意:n个城市(n <= 10000), 有m条边(m <= 40000),每一个城市有一个维护费用Cost(i),除此之外,每条边的维修费用为去掉该边后不能通信的城市对数与边权的积.这个 ...

  5. 文件宝iOS/iPhone/iPad客户端简介

    App Store地址:https://itunes.apple.com/cn/app/id1023365565?mt=8 文件宝-装机必备的文件管家,专业的rar-zip 解压工具,局域网看片神器, ...

  6. hadoop的一般端口使用

  7. 在VS2010中使用MySQL-转载

    下面这篇文章进过测试,确实可以.记下来,留作记录. http://blog.sina.com.cn/s/blog_782496390100qjcu.html

  8. 前端模块化开发的规范:AMD与CDM

    AMD, 异步模块定义. CMD,通用模块规范.

  9. TTL以及LVDS接口传输【转】

    本文转载自:http://blog.csdn.net/jscese/article/details/16860833 TTL接口:属于并行方式传输数据的接口,采用这种接口时,不必在液晶显示器的驱动板端 ...

  10. HDU1358 Period —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...