首页 文章

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

提问于
浏览
22

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

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

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

3 回答

  • 1

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

  • 35

    其中一种方法是为列定义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);
            }
        }
    }
    

    希望这有帮助,问候

  • 6

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

相关问题