基于itemource中对象的属性,如何更改WPF DataGrid中的Cell的颜色? [英] How do I change the color of Cell in WPF DataGrid based on a property of the object in itemsource?

查看:130
本文介绍了基于itemource中对象的属性,如何更改WPF DataGrid中的Cell的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 公开封闭类CellData 
{
内部字符串DisplayText
{
得到
设置;
}

public string颜色
{
get;
设置;
}

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 =BackgroundValue =Red/>
< Setter Property =ForegroundValue =White/>
< / DataTrigger>

我对XAML databidinding很新,所以任何建议都将不胜感激。谢谢。

解决方案

我猜你有一个RowData对象包含几个CellData对象,你绑定了ItemsSource DataGrid到RowData对象的列表,并且您正在使用DataGridTextColumns或其他DataGridBoundColumns绑定到RowData上的属性,也许只需使用AutoGenerateColumns =True。



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



如果您明确创建列,您可以在触发器中再次使用该列的属性:

 < DataGridTextColumn Binding ={Binding Foo}> 
< DataGridTextColumn.CellStyle>
< Style TargetType =DataGridCell>
< Style.Triggers>
<! - DataContext是行,所以启动
绑定路径与Foo属性 - >
< DataTrigger Binding ={Binding Foo.Color}值=1>
< Setter Property =BackgroundValue =Red/>
< Setter Property =ForegroundValue =White/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /DataGridTextColumn.CellStyle>
< / DataGridTextColumn>

不幸的是,这不会让您分享列之间的样式,因为它特定于列名称。如果要这样做,您可能需要创建一个自定义的DataGridColumn子类,该子类具有类型为Binding的属性,它适用于GenerateElement和GenerateEditingElement返回的对象的DataContext属性。



(使用与自己的RelativeSource的绑定,就像您在示例中所做的一样,给出了可视化树中的元素,而不是其DataContext,这将无法帮助您CellData对象。)


I have a very simple object called CellData. Is defined as:

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

    public string Color
    {
        get;
        set;
    }

    public override string ToString()
    {
        return this.DisplayText;
    }
}

I can get it to display using the WPF toolkit DataGrid just fine. However, I want to be able to change the background color of each cell based on what data is in the cell. I'm having trouble understanding what type of binding I need to do because I can't see to get to the CellData object in my DataTrigger. I have tried the following, and several other variations but I can't get it to work:

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

I am pretty new to XAML databidinding so any suggestions would be greatly appreciated. Thank you.

解决方案

I'm guessing you have a RowData object that contains several CellData objects, that you have bound the ItemsSource of the DataGrid to a list of RowData objects, and that you are using DataGridTextColumns or other DataGridBoundColumns bound to the properties on RowData, maybe just by using AutoGenerateColumns="True".

The problem is that the DataContext for the cell is actually the RowData, not the CellData. The Binding is only used for the Text property of the TextBlock and TextBox. This is useful if you want to have the triggers based on other properties of the RowData object, but makes it difficult in scenarios like yours where you have a rich data structure for the cell data.

If you are creating the columns explicitly, you can just use the property for the column again in the trigger:

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

Unfortunately, this won't let you share the style between columns since it is specific to the column name. If you want to do that, you may need to create a custom DataGridColumn subclass that has a property of type Binding that it applies to the DataContext property of the object returned by GenerateElement and GenerateEditingElement.

(Using a Binding with RelativeSource of Self as you did in your sample gives you the element in the visual tree rather than its DataContext, which won't help you get to the CellData object.)

这篇关于基于itemource中对象的属性,如何更改WPF DataGrid中的Cell的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆