如何使用WPF Toolkit Datagrid更改单元格的背景颜色

时间:2009-11-16 22:00:47

标签: wpf datagrid background wpftoolkit

我正在使用WPF工具包datagrid,我想根据单元格的内容设置单元格的背景颜色,而不是行。

为了简单起见,我们假设列被称为Foo,我希望当Foo为1时,单元格的背景为蓝色,Foo为2时为红色,Foo为3时为黄色,Foo为3时为绿色大于3。

如果我能做到这一点,我很确定我能解决任何需要处理的更复杂案件。

4 个答案:

答案 0 :(得分:35)

您可以使用Styles和DataTriggers执行此操作。只需使用默认的背景属性设置ElementStyle,在本例中为Green,并为其他情况添加DataTriggers:

<DataGridTextColumn Binding="{Binding WhateverIWantToDisplay}" >
  <DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">

      <Setter Property="Background" Value="Green" />

      <Style.Triggers>
        <DataTrigger Binding="{Binding Foo}" Value="1">
          <Setter Property="Background" Value="Blue" />
        </DataTrigger>

        <DataTrigger Binding="{Binding Foo}" Value="2">
          <Setter Property="Background" Value="Red" />
        </DataTrigger>

        <DataTrigger Binding="{Binding Foo}" Value="2">
          <Setter Property="Background" Value="Yellow" />
        </DataTrigger>

      </Style.Triggers>
    </Style>
  </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

另一种方法是使用与转换器的绑定:

<DataGridTextColumn Binding="{Binding WhateverIWantToDisplay}" >
  <DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">

      <Setter Property="Background"
        Value="{Binding Foo, Converter={x:Static my:FooToColorConverter.Instance}}" />

    </Style>
  </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

使用此转换器:

public class FooToColorConverter : IValueConverter
{
  public static readonly IValueConverter Instance = new FooToColorConverter();
  public object Convert(object value, ...
  {
    int foo = (int)value;
    return
      foo==1 ? Brushes.Blue :
      foo==2 ? Brushes.Red :
      foo==3 ? Brushes.Yellow :
      foo>3 ? Brushes.Green :
        Brushes.Transparent;  // For foo<1
  }
  public object ConvertBack(...
  {
    throw new NotImplementedException();
  }
}

请注意,回答serge_gubenko给出也可以,但仅当您的Foo属性值永远不会改变时。这是因为Color属性getter只会被调用一次。他的解决方案可以通过将Color更改为只读DependencyProperty并在分配Foo时更新它来改进,但在数据模型中使用像颜色这样的特定于UI的信息通常是个坏主意,因此不建议这样做。 p>

答案 1 :(得分:6)

如何执行此操作的方法之一是为列定义ElementStyle,然后将textblock背景绑定到datagrid行后面的data元素的color属性。这是一个例子:

DataGridTextColumn xaml:

<DataGridTextColumn Width="SizeToCells"   
                       MinWidth="150" 
                       Binding="{Binding Name}">

    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextBlock.Background" Value="{Binding Color}" />
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

数据项声明:

public class TestItem
{
    public TestItem(int foo)
    {
        Foo = foo;
    }

    public int Foo { get; set; }
    public Brush Color
    {
        get
        {
            Color color = Colors.Green;
            switch (Foo)
            {
                case 1: color = Colors.Red; break;
                case 2: color = Colors.Yellow; break; 
            }
            return new SolidColorBrush(color);
        }
    }
}

希望这有帮助,尊重

答案 2 :(得分:1)

如果您的项目从INotifyPropertyChanged继承,那么serge_gubenko将运行良好,然后您的属性将更改调用NotifyPropertyChanged(“yourproperty”)

答案 3 :(得分:0)

一种略有不同的方法,而不是将TextBlock元素(通常在控件周围留下边界)而不是DataGridCell本身。就我而言,我已经有想要继承的样式,我只需要根据值更改背景颜色:

<DataGridTextColumn 
    Width="*"
    Header="Status"
    Binding="{Binding EventStatus, Converter={StaticResource DescriptionAttributeConverter}}"
    HeaderStyle="{StaticResource DataGridColumnHeaderStyleCenterAligned}">

    <DataGridTextColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource DataGridCellStyleCenterAligned}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding EventStatus}" Value="1">
                    <Setter Property="Background" Value="Green" />
                </DataTrigger>

                <DataTrigger Binding="{Binding EventStatus}" Value="2">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

我的情况可能对人们有用。