我正在尝试创建一个自定义DataGrid,我可以根据单元格值格式化单个单元格(即;负值为红色文字,后置为绿色)ala这种方法......
How to get Binding value of current cell in a WPFToolkit DataGrid
我还需要将值从负值转换为括号(即:-2.34到(2.34))。 我有继承/过度工作。我的问题是,如何在重写的GenerateElement方法中访问单元格中的值。
提前致谢, 菲尔
答案 0 :(得分:2)
我的做法错了。我需要使用IValueConverter并绑定Text和Foreground属性,如此...
type FixedDecimalConverter() =
interface IValueConverter with
member this.Convert(value, targetType, parameter, culture) =
match value :?> Double with
| Globals.DataGridHelper.IsNegative x ->
sprintf "(%.2f%%)" (Math.Abs x) :> obj
| Globals.DataGridHelper.IsPositive x ->
sprintf "%.2f%%" x :> obj
member this.ConvertBack(value, targetType, parameter, culture) = raise <| NotImplementedException()
type ForegroundValueConverter() =
interface IValueConverter with
member this.Convert(value, targetType, parameter, culture) =
match value :?> Double with
| Globals.DataGridHelper.IsNegative x -> Globals.DataGridHelper.redBrush :> obj
| Globals.DataGridHelper.IsPositive x -> Globals.DataGridHelper.greenBrush :> obj
member this.ConvertBack(value, targetType, parameter, culture) = raise <| NotImplementedException()
和Xaml ......
<data:DataGridTemplateColumn Header="YTD v. Sector" x:Name="YTDvSector" Visibility="Collapsed">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Right" VerticalAlignment="Center"
Text="{Binding ytdSectorReturn.value, Converter={StaticResource fcFixedDecimalConverter}}"
Foreground="{Binding ytdSectorReturn.value, Converter={StaticResource fcForegroundValueConverter}}"/>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
和管道......
<UserControl.Resources>
<y:FixedDecimalConverter x:Key="fcFixedDecimalConverter" />
<y:ForegroundValueConverter x:Key="fcForegroundValueConverter" />
</UserControl.Resources>