2009-11-16 202 views
22

我正在使用WPF工具包数据网格,我想设置单元格的背景颜色,而不是行,基于内容的细胞。如何更改单元格的背景颜色使用WPF工具包Datagrid

为了简单起见,可以说这个列被称为Foo,并且我希望当Foo为1时单元格的背景为蓝色,当Foo为2时为红色,当Foo为3时为黄色,并且当Foo时为绿色大于3.

如果我可以这样做,我敢肯定我可以解决任何我需要处理的更复杂的情况。

回答

33

您与样式和DataTriggers做到这一点。只需设置你的ElementStyle与您的默认背景属性,在这种情况下绿色,并添加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属性值NE Ver变化。这是因为Color属性获取器只会被调用一次。可以通过将Color更改为只读DependencyProperty并在分配Foo时对其更新来改进其解决方案,但在数据模型中使用特定于UI的信息(如颜色)通常不是个好主意,因此不建议这样做。

+0

非常感谢。我发现WPF如此令人沮丧,因为它倾向于隐藏错误,并且在没有正确代码的情况下不做任何事情。但是当它工作时,当你知道你在做什么时,它的闪电快速且易于使用。我会诚实地说WPF具有我使用过的任何技术最犀利的学习曲线。 无论如何,再次感谢。我与转换器一起使用,因为它给了我最大的灵活性。 – 2009-11-17 16:26:02

+0

美丽.. !!! +1从我身边..! – samar 2011-06-23 13:27:33

6

如何做到这一点的方法之一是为列定义ElementStyle,然后将textblock背景绑定到datagrid行后面的数据元素的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); 
     } 
    } 
} 

希望这会有所帮助,至于

+0

如果foo的值就会显示网格后发生了改变,这将不会工作:单元格颜色不会更新。查看我的答案了解更多详情。 – 2009-11-17 06:07:07

+0

而且如果Foo在显示网格时发生变化(例如,由于数据绑定)。 – greenoldman 2010-08-25 12:24:55

+0

那么它的老,但你需要解决它的一切是由类TestItem实现INotifyPropertyChanged,以通知UI的颜色变化。 – Tafari 2013-10-28 07:07:03

1

的serge_gubenko将工作做好,如果你的项目继承INotifyPropertyChanged的那么当你的财产将改变调用NotifyPropertyChanged(“yourproperty”)

相关问题