WPF DataGrid foreground 绑定问题
初学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:
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://stackoverflow.com/questions/5926849/datagridtextcolumn-how-to-bind-isreadonly
希望可以帮到大家
WPF DataGrid foreground 绑定问题的更多相关文章
- (WPF) DataGrid之绑定
通过ObservableCollection 绑定到 DataGrid. 1. 前台Xaml. <DataGrid x:Name="dgMeasurements" Horiz ...
- WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.
WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次 悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...
- WPF DataGrid绑定一个组合列
WPF DataGrid绑定一个组合列 前台: <Page.Resources> <local:InfoConverter x:Key="converter& ...
- WPF DataGrid 绑定行双击行命令
WPF DataGrid 绑定行双击行命令 <DataGrid ...> <DataGrid.InputBindings> <MouseBinding MouseActi ...
- WPF dataGrid下的ComboBox的绑定
WPF dataGrid下的ComboBox的绑定 Wpf中dataGrid中的某列是comboBox解决这个问题费了不少时间,不废话了直接上代码 xaml 代码 <DataGridTempla ...
- WPF DataGrid 双击行 获得绑定数据
原文:WPF DataGrid 双击行 获得绑定数据 1)增加事件 2)增加对象获取 1)事件代码 Datagrid 增加事件 MouseDoubleClick="dataGrid_Mous ...
- WPF Datagrid 动态生成列 并绑定数据
原文:WPF Datagrid 动态生成列 并绑定数据 说的是这里 因为列头是动态加载的 (后台for循环 一会能看到代码) 数据来源于左侧列 左侧列数据源 当然num1 属于临时的dome使用 可 ...
- WPF DataGrid 绑定数据及时更新的处理
原文:WPF DataGrid 绑定数据及时更新的处理 默认情况下datagrid 绑定数据源后,在界面编辑某一列后,数据不会及时更新到内存对象中.如在同一行上有一个命令对来获取 当前选中行(内存对象 ...
- WPF DATAGrid 空白列 后台绑定列 处理
原文:WPF DATAGrid 空白列 后台绑定列 处理 AutoGenerateColumns <DataGrid x:Name="dataGrid" Margin=&qu ...
随机推荐
- hive cli 启动缓慢问题
hive-0.13.1启动缓慢的原因 发现时间主要消耗在以下3个地方: 1. hadoopjar的时候要把相关的jar包上传到hdfs中(这里大概消耗5s,hive0.11一样,这个地方不太好优化) ...
- Intel processor brand names-Xeon,Core,Pentium,Celeron----Pentium
http://en.wikipedia.org/wiki/Pentium Pentium From Wikipedia, the free encyclopedia This article ...
- Qt浅谈之二十一log调试日志
一.简单介绍 近期因调试code时,想了解程序的流程,但苦于没有一个简易的日志记录,不停使用qDebug打印输出,而终于提交代码时得去多次删除信息打印,有时还会出现新改动的代码分不清是哪些部分.而使用 ...
- iOS APP第一次上架遇到的问题
现在苹果审核时越来越严了,我们有两个APP时同时上线的,代码用的也是一套的.但是有其中一个是第一次发布所以就拒了,信息就是下图.大概意思是用到支付了吗?用户是怎么来的.值需要把这些信息回复了.就OK ...
- C# does not contain a constructor that takes no parameter
C# 中子类要重用父类的构造函数时, 一般会在子类构造函数后面调用 : base(paratype, para). 如果父类有一个參数个数为1的构造函数, 没有 0 參构造函数. 子类想要重用这个构造 ...
- 树的深度优先遍历和广度优先遍历的原理和java实现代码
import java.util.ArrayDeque; public class BinaryTree { static class TreeNode{ int value; TreeNode le ...
- maven统一配置
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...
- hdu acm 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191 题目意思:有 资金 n 和 m 种类型的大米,对第 i 种类型的大米,价格.数量.袋数分别是: ...
- WAS:Thread "server.startup : 1" (00000020) and may be hung异常
有现场server启动时,启动不了,后台报错如下: [// ::: CST] ThreadMonitor W WSVR0605W: Thread ) has been active milliseco ...
- bzoj 2301 [HAOI2011]Problem b(莫比乌斯反演+分块优化)
题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 1≤n≤50000,1≤a≤b≤50000, ...