【问题标题】:DataGridCell style, how to reference the cells value to bind to a converter?DataGridCell 样式,如何引用单元格值绑定到转换器?
【发布时间】:2011-12-07 22:51:38
【问题描述】:

我有一个转换器,它接受一个十进制值并将其转换为画笔(红色表示负输入,黑色表示正输入)。我还创建了一种样式,我想将其应用于所有采用十进制值的DataGridTextColumn。如果我为每个 DataGridTextColumn 内联样式,我可以简单地在绑定表达式中的 Datacontext 上指定相关属性。但是我不想内联样式并将其简单地作为资源,这样我就可以将 CellStyle 设置为资源。问题是我不知道在 Foreground 属性的值的绑定中放入什么。我希望能够将它绑定到应用它的单元格所绑定的值。

这是我所拥有的:

    <Window.Resources>
        <!-- This converter takes a decimal value and returns a brush -->
        <conv:NumericValueBrushColorConverter x:Key="NumericValueBrushColorConverter"></conv:NumericValueBrushColorConverter>

        <Style x:Key="CurrencyStyle" TargetType="DataGridCell">
            <Setter Property="HorizontalAlignment"  Value="Right" />
            <Setter Property="Foreground"  Value="{Binding WhatGoesHere, Converter={StaticResource NumericValueBrushColorConverter}}"></Setter>
        </Style>
    </Window.Resources>             


    <DataGrid ItemsSource="{Binding CashReport}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Beginning Cash Available" Binding="{Binding BeginningBalance, StringFormat={}{0:C}}" CellStyle="{StaticResource CurrencyStyle}" />
            <DataGridTextColumn Header="Ending Cash Available" Binding="{Binding EndingBalance, StringFormat={}{0:C}}" CellStyle="{StaticResource CurrencyStyle}" />
        </DataGrid.Columns>
    </DataGrid>

更新 #1

根据 Jefim 的建议,我应该尝试使用 ElementStyle,因为它直接在呈现的 TextBlock 上工作,而不是 (CellStyle) 包含 TextBlock 的内容控件(无论是什么)。

    <Style x:Key="CurrencyStyle" TargetType="TextBlock">
        <Setter Property="HorizontalAlignment"  Value="Right" />
        <Setter Property="Foreground" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Text, Converter={StaticResource NumericValueBrushColorConverter}}"/>
    </Style>

    <DataGrid ItemsSource="{Binding CashReport}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Beginning Cash Available" Binding="{Binding BeginningBalance, StringFormat={}{0:C}}" ElementStyle="{StaticResource CurrencyStyle}" />
            <DataGridTextColumn Header="Ending Cash Available" Binding="{Binding EndingBalance, StringFormat={}{0:C}}" ElementStyle="{StaticResource CurrencyStyle}" />
        </DataGrid.Columns>
    </DataGrid> 

这似乎有效,但是当我跟踪转换器的 Convert 方法时,它似乎触发了大约 28 次左右,并且将空值传递给 Convert 方法。之后,所有值都按预期流动。当网格渲染一切正常时,没有空单元格。什么会在前 20 多次没有值的情况下执行我的转换器?

更新 #2 我相信我当前的问题与原始问题无关,因此我将其移至:IValueConverter executes more times than expected

【问题讨论】:

    标签: wpf wpfdatagrid


    【解决方案1】:

    您可以(并且可能应该)使用 ElementStyle / EditingElementStyle 属性 - 它们将允许您设置单元格内的 TextBlockTextBox 的样式。例如:

    <Style x:Key="CurrencyStyle" TargetType="TextBlock">
        <Setter Property="HorizontalAlignment"  Value="Right" />
        <Setter Property="Foreground" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Text, Converter={StaticResource NumericValueBrushColorConverter}}"/>
    </Style>
    
    <DataGrid ItemsSource="{Binding CashReport}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Beginning Cash Available" Binding="{Binding BeginningBalance, StringFormat={}{0:C}}" ElementStyle="{StaticResource CurrencyStyle}" />
            <DataGridTextColumn Header="Ending Cash Available" Binding="{Binding EndingBalance, StringFormat={}{0:C}}" ElementStyle="{StaticResource CurrencyStyle}" />
        </DataGrid.Columns>
    </DataGrid> 
    

    更新

    将代码更新为与问题更新 #1 中的相同,以便查看答案的人会看到正确版本的代码(由 e36M3 提供)

    【讨论】:

    • 这不起作用,因为 DataGridCell 上没有“ElementStyle”属性。但是,使用 DataTextColumn 的 Elementstyle 时需要注意一点,所以我进行了调整并更新了我的原始帖子。我仍然有一个问题,为什么转换器在实际值开始出现之前以空值触发几次。
    • 我不确定我是否理解正确,但我认为很可能您正在多次设置属性(例如,您用零初始化这些属性的值)。否则很难在没有额外信息的情况下判断。尝试检查堆栈跟踪。
    • Jefim 我刚刚用我正在做的事情更新了我的原始帖子。也许这会有所帮助。
    • Jefim,我认为我当前的问题与原始问题无关,因此我为此发布了另一个问题。我会认为这个答案是正确的答案。顺便说一句,如果您使用 TargetType="DataGridTextColum",我认为 XAML 会抱怨,但它的工作方式与我在更新中发布的方式相同。
    • 好吧,我很高兴你最终弄明白了。很高兴知道。您的更新 #2 中的链接也是如此。是的,XAML 会抱怨 DataGridTextColumn,因为目标类型必须从 FrameworkElement 派生,而 DataGridTextColumn 从 DependencyObject 派生,我完全忘记了这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 2014-10-22
    相关资源
    最近更新 更多