如何更改使用WPF工具包的数据网格中的单元格的背景颜色 [英] How do I change the background color of a cell using WPF Toolkit Datagrid

查看:107
本文介绍了如何更改使用WPF工具包的数据网格中的单元格的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF工具包数据网格,我想设置一个单元格,而不是行的背景颜色的基础上,单元格的内容。

I'm using the WPF toolkit datagrid, and I'd like to set the background color of a cell, not the row, based on the content of the cell.

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

For the sake of simplicity, lets say the column is called Foo and I'd like the background of the cell to be blue when Foo is 1, red when Foo is 2, Yellow when Foo is 3 and Green when Foo is greater than 3.

如果我能做到这一点,我是pretty相信我能解决任何复杂的情况下,我需要去处理。

If I can do that, I'm pretty sure I can solve any of the more complex cases that I need to deal with.

推荐答案

您这样做与样式和DataTriggers。只需设置您的ElementStyle与您的默认背景属性,在这种情况下绿色,并添加DataTriggers对于其他情况:

You do this with Styles and DataTriggers. Just set your ElementStyle with your default background property, in this case Green, and add DataTriggers for the other cases:

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

另一种方法是使用具有一个转换器的结合:

Another approach is to use a binding with a converter:

<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属性值永远不会改变。这是因为颜色属性getter只会被调用一次。他的解决方案可以通过改变颜色,以只读的DependencyProperty和更新它,每当富分配得到改善,但它通常是一个坏主意,有像你的数据模型的颜色UI相关的信息,所以不推荐。

Note that answer serge_gubenko gave will work as well, but only if your Foo property value never changes. This is because the Color property getter will only be called once. His solution can be improved by changing Color to a read-only DependencyProperty and updating it whenever Foo is assigned, but it is generally a bad idea to have UI-specific information like colors in your data model, so it is not recommended.

这篇关于如何更改使用WPF工具包的数据网格中的单元格的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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