初学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. LeetCode_Lowest Common Ancestor of a Binary Search Tree (Binary Tree)

    Lowest Common Ancestor of a Binary Search Tree 一.题目描写叙述 二.思路及代码 二叉搜索树有个性质:左子树的值都比根节点小,右子树的值比根节点大.那么我 ...

  2. AppCan移动应用开发平台新增9个超有用插件(内含演示样例代码)

    使用AppCan平台进行移动开发.你所须要具备的是Html5+CSS +JS前端语言基础.此外.Hybrid混合模式应用还需结合原生语言对功能模块进行封装,对于没有原生基础的开发人员,怎样实现App里 ...

  3. openERP邮件(发信、收信)

    openERP里的邮件处理主要有个2个模块处理 mail -核心 fetchmail -接收邮件     Alias domain和alias name     配置domain alias. Set ...

  4. Codeforces Round #258 (Div. 2/A)/Codeforces451A_Game With Sticks

    解题报告 http://blog.csdn.net/juncoder/article/details/38102263 n和m跟木棍相交,问一人取一交点(必须是交点.且取完后去掉交点的两根木棍),最后 ...

  5. CF 568A(Primes or Palindromes?-暴力推断)

    A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input st ...

  6. Jupyter notebook 使用Turorial

    The cell magics in IPython http://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/note ...

  7. 多媒体开发之---h264 取流解码分析

    1. nalu_unit_type = *((unsigned char *)pEmptyBuf->bufVirtAddr+4); nalu_unit_type = nalu_unit_type ...

  8. NettyIO

  9. char* strcpy( char* dest, const char* src ), int binary_search(int *arr, int key, int n), 可能的实现

    #include <stdio.h> char* stringCopy( char* dest, const char* src ) { size_t i = 0; while (dest ...

  10. 20170225 ABAP获取字符串长度/字节长度

    函数YGET_CHAR_LONG: FUNCTION YGET_CHAR_LONG. *"-------------------------------------------------- ...