C# 如何根据itemsource中对象的属性更改WPF DataGrid中单元格的颜色?

C# 如何根据itemsource中对象的属性更改WPF DataGrid中单元格的颜色?,c#,wpf,datagrid,wpftoolkit,C#,Wpf,Datagrid,Wpftoolkit,我有一个非常简单的对象叫做CellData。定义如下: public sealed class CellData { internal string DisplayText { get; set; } public string Color { get; set; } public override string ToString() { retur

我有一个非常简单的对象叫做CellData。定义如下:

public sealed class CellData
{
    internal string DisplayText
    {
        get;
        set;
    }

    public string Color
    {
        get;
        set;
    }

    public override string ToString()
    {
        return this.DisplayText;
    }
}
我可以使用WPF工具包DataGrid很好地显示它。但是,我希望能够根据单元格中的数据更改每个单元格的背景颜色。我很难理解需要执行哪种类型的绑定,因为我看不到如何访问DataTrigger中的CellData对象。我尝试了以下方法,以及其他几种方法,但都无法成功:

            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(CellData).Color, Mode=OneWay}" Value="1">
                <Setter Property="Background" Value="Red" />
                <Setter Property="Foreground" Value="White" />
            </DataTrigger>


我对XAML数据库非常陌生,因此任何建议都将不胜感激。谢谢。

您可以使用ValueConverter:在单元格的背景色和对象的颜色属性之间创建绑定,然后添加ValueConverter,以确保数据正确转换为设置背景所需的对象。

您可以使用ValueConverter:在单元格的背景颜色和对象的颜色属性之间创建绑定,然后添加一个ValueConverter,以确保您的数据正确地转换为设置背景所需的对象。

我猜您有一个包含多个CellData对象的RowData对象,您已将DataGrid的ItemsSource绑定到RowData对象列表,您正在使用DataGridTextColumns或其他绑定到RowData属性的DataGridBoundColumns,可能只是使用AutoGenerateColumns=“True”

问题是单元格的DataContext实际上是RowData,而不是CellData。绑定仅用于TextBlock和TextBox的Text属性。如果希望触发器基于RowData对象的其他属性,这将非常有用,但在单元格数据具有丰富数据结构的情况下,这将非常困难

如果要显式创建列,只需在触发器中再次使用该列的属性即可:

<DataGridTextColumn Binding="{Binding Foo}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <!-- DataContext is the row, so start 
                     binding path with Foo property -->
                <DataTrigger Binding="{Binding Foo.Color}" Value="1">
                    <Setter Property="Background" Value="Red" />
                    <Setter Property="Foreground" Value="White" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

不幸的是,这不允许在列之间共享样式,因为它是特定于列名的。如果要这样做,可能需要创建一个自定义DataGridColumn子类,该子类具有绑定类型的属性,该属性应用于GenerateElement和GenerateEditingElement返回的对象的DataContext属性


(使用与RelativeSource of Self的绑定,就像您在示例中所做的那样,为您提供了可视化树中的元素,而不是它的DataContext,这不会帮助您访问CellData对象。)

我猜您有一个包含多个CellData对象的RowData对象,您已经将DataGrid的ItemsSource绑定到RowData对象列表,并且您正在使用DataGridTextColumns或其他绑定到RowData属性的DataGridBoundColumns,可能只是使用AutoGenerateColumns=“True”

问题是单元格的DataContext实际上是RowData,而不是CellData。绑定仅用于TextBlock和TextBox的Text属性。如果希望触发器基于RowData对象的其他属性,这将非常有用,但在单元格数据具有丰富数据结构的情况下,这将非常困难

如果要显式创建列,只需在触发器中再次使用该列的属性即可:

<DataGridTextColumn Binding="{Binding Foo}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <!-- DataContext is the row, so start 
                     binding path with Foo property -->
                <DataTrigger Binding="{Binding Foo.Color}" Value="1">
                    <Setter Property="Background" Value="Red" />
                    <Setter Property="Foreground" Value="White" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

不幸的是,这不允许在列之间共享样式,因为它是特定于列名的。如果要这样做,可能需要创建一个自定义DataGridColumn子类,该子类具有绑定类型的属性,该属性应用于GenerateElement和GenerateEditingElement返回的对象的DataContext属性


(在示例中使用与RelativeSource of Self的绑定会给您提供可视化树中的元素,而不是它的DataContext,这不会帮助您访问CellData对象。)

Maurizio Reginelli-我不确定我是否遵循了。Quartermeister,我要做的是创建一个DataTable,然后将DataGrid的DataContext设置为DataTable,并将AutoGenerate设置为true。问题是列和行都是在运行时创建的。最重要的是,我不想将样式应用于列,因为我不想更改列中所有单元格的颜色,只是一些单元格的颜色。我怀疑这里的问题是我还没有完全理解绑定和XAML是如何工作的。我会继续研究,但如果你有任何其他建议,那就太好了。@Nick:对列应用单元格样式将分别对每个单元格应用触发器,所以这可能是你想要的。如果使用自动生成的列,这将很困难,因为单元格的DataContext将是DataRow,而不是单个单元格的值。只有TextBlock或TextBox的Text属性绑定到特定列。通过处理AutoGeneratingColumn事件并基于当前列应用带有触发器的样式,您可能可以做到这一点,但随后您将在代码中执行许多表示层逻辑。现在一切都很好,谢谢!这很奇怪,也许是因为我对WPF不太熟悉,但我觉得WPF数据网格是一种倒退。Quartermeister,我要做的是创建一个DataTable,然后将DataGrid的DataContext设置为DataTable,并将AutoGenerate设置为true。问题是列和行都是在运行时创建的。除此之外