使用多重绑定更改 WPF DataGrid 单元格的前景色时出现问题

发布于 2024-11-04 18:48:43 字数 1251 浏览 6 评论 0原文

给定特定单元格内容的值,我正在尝试更改 DataGridCell 的前景色,而不是整个行的前景色。例如,如果单元格的日期值已过期,那么我想将前景设置为红色。我几乎正在使用 MultiBinding 从数据网格中提取两个值,并通过实现 IMultiValueConverter 的值转换器对其进行解析。

简而言之,无需进入转换器本身,我编写了以下 XAML:

<GlobalTasks:CellHighlighterConverter x:Key="cellHighlighterConverter"/>

<Style x:Key="CellHighlighterStyle">
    <Setter Property="my:ExtendedDataGrid.Foreground">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource cellHighlighterConverter}">
                <MultiBinding.Bindings>
                    <Binding RelativeSource="{RelativeSource self}"/>
                    <Binding Path="Row" Mode="OneWay"/>
                </MultiBinding.Bindings>
            </MultiBinding>
        </Setter.Value>
    </Setter>            
</Style>

然后添加到我的 Datagrid 中:CellStyle="{StaticResource CellHighlighterStyle}"

现在,我的转换器将第一个 Value[0] 属性视为 DataGridCell,但第二个值是某种组成的 DependencyProperty。当我真正想要获取行时,它就像创建了默认或虚拟属性一样。事实上,我真的很想获取与该行关联的类对象。我的 DataGrid 绑定到List<类对象>

我确信我就在几英寸之外。如何解析 DataGrid 行以便提取字段的值,或者解析绑定到该行或列本身的对象以便返回我想要返回的颜色?或者,是否有更好的方法让我有选择地更改给定值的 DataGrid 单元格的属性?

I am trying to change the Foreground colour of a DataGridCell, NOT the entire row, given the value of the specific cell contents. For example, if the date value of the cell is out of date then I want to set the Foreground to red. I am almost where I am using MultiBinding to extract two values from the data grid and parsing it through value converter which implements IMultiValueConverter.

In short, and without then need to go into the converter itself, I have written the following XAML:

<GlobalTasks:CellHighlighterConverter x:Key="cellHighlighterConverter"/>

<Style x:Key="CellHighlighterStyle">
    <Setter Property="my:ExtendedDataGrid.Foreground">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource cellHighlighterConverter}">
                <MultiBinding.Bindings>
                    <Binding RelativeSource="{RelativeSource self}"/>
                    <Binding Path="Row" Mode="OneWay"/>
                </MultiBinding.Bindings>
            </MultiBinding>
        </Setter.Value>
    </Setter>            
</Style>

I then added to my Datagrid : CellStyle="{StaticResource CellHighlighterStyle}".

Now, my converter sees the first Value[0] property as the DataGridCell but the second value is some sort of made up DependencyProperty. It’s like it has created a default or dummy property when I actually want to get the row. In fact, I would really like to get the class object associated to that row. My DataGrid is bound to a List<of a class object>.

I am sure that I am inches away. How do I parse either the DataGrid Row so that I can extract the value of the field, or the object bound to that row or column itself in order for me to return the colour I want to return? OR, is there a better way for me to selectively alter the properties of a DataGrid Cell given the value?

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

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

发布评论

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

评论(1

不羁少年 2024-11-11 18:48:43

单元格的 DataContext 应该是行的数据对象,尝试将第二个绑定更改为此:

<Binding />

直接绑定到 DataContext。

The DataContext of the cell should be the data-object of the row, try changing the second binding to this:

<Binding />

Which binds directly to the DataContext.

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