DataGridCell样式,如何引用单元格值绑定到转换器?

发布于 2024-12-20 03:13:07 字数 2724 浏览 2 评论 0原文

我有一个转换器,它采用十进制值并将其转换为画笔(红色表示负输入,黑色表示正输入)。我还创建了一个样式,我想将其应用于所有采用十进制值的 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) 内容控件(无论是什么)保存文本块。

    <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 执行比预期次数多

I have a converter that takes a decimal value and converts it to a brush (red for negative and black for positive input). I also created a style that I want to apply to all DataGridTextColumn that will take decimal values. If I inline the style for each DataGridTextColumn I can simply specify the related property on the Datacontext in the binding expression. However I don't want to inline the style and simply have it as a resource, that way I can set the CellStyle to the resource. The problem is I don't know what to put in the Binding of the value for the Foreground property. I want to be able to bind it to value that the cell to which it's being applied is bound to.

Here is what I have:

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

Update #1

As per Jefim's suggestion I should attempt to use the ElementStyle as it works directly on the rendered TextBlock as opposed to (CellStyle) the content control (whatever that is) that holds the 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> 

This seems to work, however when I track my converter's Convert method it seems to fire about 28 times or so with empty value being passed to the Convert method. After that all values flow as expected. When the grid renders everything looks right, there are no empty cells. What executes my converter the first 20+ times without values?

Update #2
I believe my current issue is unrelated to the original question so I moved it out to: IValueConverter executes more times than expected

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

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

发布评论

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

评论(1

婴鹅 2024-12-27 03:13:07

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

<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 提供)

You can (and probably should) use the ElementStyle / EditingElementStyle properties - they will allow you to style the TextBlock and TextBox that are within the cell. E.g.:

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

UPDATE

Updated the code to the same as in the question's update #1 so that people who look at the answer would see the correct version of code (by e36M3)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文