继承自 DataGridTextColumn 并重写GenerateElement

发布于 2024-09-04 00:14:39 字数 383 浏览 11 评论 0 原文

我正在尝试创建一个自定义 DataGrid,在其中我可以根据单元格值格式化各个单元格(即;红色文本表示负值,绿色文本表示正值),就像这种方法一样...

如何获取 WPFToolkit DataGrid 中当前单元格的绑定值

我还需要将值从负数转换为带括号的值(即 -2.34 到 (2.34))。 我已经让继承/覆盖工作了。我的问题是,如何访问重写的GenerateElement方法中单元格中的值。

提前致谢, 菲尔

I'm attempting to create a custom DataGrid where I can format individual cells based on the cell value (ie; red text for negative values, green for postitive) ala this approach...

How to get Binding value of current cell in a WPFToolkit DataGrid

I also need to convert the values from negative to parenthesised (ie; -2.34 to (2.34)).
I've got the inheritance/overide working. My question is, how do I get access to the values in the cells in the overridden GenerateElement method.

Thanks in advance,
Phil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毅然前行 2024-09-11 00:14:39

我的做法是错误的。我需要使用 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>

My approach was wrong. I needed to use IValueConverter and bind the Text and Foreground properties like so...

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()

And the 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>

And the plumbing...

<UserControl.Resources>
        <y:FixedDecimalConverter x:Key="fcFixedDecimalConverter" />
        <y:ForegroundValueConverter x:Key="fcForegroundValueConverter" />
    </UserControl.Resources>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文