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

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

问题描述

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

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

如果我能做到,我很确定我可以解决我需要处理的任何更复杂的案例.

解决方案

您可以使用 Styles 和 DataTriggers 来做到这一点.只需使用默认背景属性设置 ElementStyle,在本例中为 Green,并为其他情况添加 DataTriggers:

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

使用此转换器:

公共类 FooToColorConverter : IValueConverter{公共静态只读 IValueConverter 实例 = 新 FooToColorConverter();公共对象转换(对象值,...{int foo = (int) 值;返回foo==1 ?Brushes.Blue :foo==2 ?Brushes.Red :foo==3 ?Brushes.Yellow :foo>3 ?Brushes.Green :刷子.透明;//对于 foo<1}公共对象 ConvertBack(...{抛出新的 NotImplementedException();}}

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

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.

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.

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

解决方案

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>

with this converter:

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();
  }
}

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 Toolkit Datagrid 更改单元格的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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