【问题标题】:Problem changing the Foreground colour of a WPF DataGrid Cell using MultiBinding使用 MultiBinding 更改 WPF DataGrid 单元格的前景色时出现问题
【发布时间】:2011-08-17 03:14:22
【问题描述】:

在给定特定单元格内容的值的情况下,我正在尝试更改 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&lt;of a class object&gt;

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

【问题讨论】:

    标签: wpf datagrid multibinding


    【解决方案1】:

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

    <Binding />
    

    直接绑定到 DataContext。

    【讨论】:

    • 绝对正确。感谢您的回复。效果很好。
    猜你喜欢
    • 1970-01-01
    • 2020-04-18
    • 2018-09-26
    • 2021-03-10
    • 2011-10-15
    • 2010-12-17
    • 1970-01-01
    • 2013-01-28
    • 2015-05-14
    相关资源
    最近更新 更多