根据值更改DataGrid单元格颜色 [英] Change DataGrid cell colour based on values

查看:266
本文介绍了根据值更改DataGrid单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF数据网格,我想要根据值不同的单元格颜色。我已经下载了我的xaml的代码

  Style TargetType =DataGridCell
/ pre>

而不是选择一个单元格只是选择所有行?我缺少什么?

解决方案

如果您尝试设置 DataGrid.CellStyle DataContext将是行,所以如果要更改基于一个单元格的颜色,可能最容易在特定列中执行此操作,特别是因为列可以具有不同的内容,例如TextBlocks,ComboBoxes和CheckBoxes。以下是将 Name 的所有单元格设置为浅绿色的示例,其中 John

 < DataGridTextColumn Binding ={Binding Name}> 
< DataGridTextColumn.ElementStyle>
< Style TargetType ={x:Type TextBlock}>
< Style.Triggers>
< Trigger Property =TextValue =John>
< Setter Property =BackgroundValue =LightGreen/>
< / Trigger>
< /Style.Triggers>
< / Style>
< /DataGridTextColumn.ElementStyle>
< / DataGridTextColumn>






您还可以使用 ValueConverter 来更改颜色。 p>

  public class NameToBrushConverter:IValueConverter 
{
public object Convert(object value,键入targetType,对象参数,System.Globalization.CultureInfo文化)
{
string input = value as string;
switch(input)
{
caseJohn:
return Brushes.LightGreen;
default:
return DependencyProperty.UnsetValue;
}
}

public object ConvertBack(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
throw new NotSupportedException ();
}
}

用法:

 < Window.Resources> 
< local:NameToBrushConverter x:Key =NameToBrushConverter/>
< /Window.Resources>
...
< DataGridTextColumn Binding ={Binding Name}>
< DataGridTextColumn.ElementStyle>
< Style TargetType ={x:Type TextBlock}>
< Setter Property =BackgroundValue ={Binding Name,Converter = {StaticResource NameToBrushConverter}}/>
< / Style>
< /DataGridTextColumn.ElementStyle>
< / DataGridTextColumn>






另一个选项是直接绑定背景到返回分别彩色画笔的属性。您必须在颜色依赖的属性设置器中触发属性更改通知。



例如

  public string Name 
{
get {return _name; }
set
{
if(_name!= value)
{
_name = value;
OnPropertyChanged(Name);
OnPropertyChanged(NameBrush);
}
}
}

public Brush NameBrush
{
get
{
switch(Name)
{
caseJohn:
return Brushes.LightGreen;
默认值:
break;
}

return Brushes.Transparent;
}
}


I have got a WPF datagrid and I want diffrent cell colours according to values. I have got below code on my xaml

Style TargetType="DataGridCell"

but instead of selecting a cell only is selecting all row? What am I missing?

解决方案

If you try to set the DataGrid.CellStyle the DataContext will be the row, so if you want to change the colour based on one cell it might be easiest to do so in specific columns, especially since columns can have varying contents, like TextBlocks, ComboBoxes and CheckBoxes. Here is an example of setting all the cells light-green where the Name is John:

<DataGridTextColumn Binding="{Binding Name}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <Trigger Property="Text" Value="John">
                    <Setter Property="Background" Value="LightGreen"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>


You could also use a ValueConverter to change the colour.

public class NameToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string input = value as string;
        switch (input)
        {
            case "John":
                return Brushes.LightGreen;
            default:
                return DependencyProperty.UnsetValue;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

Usage:

<Window.Resources>
    <local:NameToBrushConverter x:Key="NameToBrushConverter"/>
</Window.Resources>
...
<DataGridTextColumn Binding="{Binding Name}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background" Value="{Binding Name, Converter={StaticResource NameToBrushConverter}}"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>


Yet another option is to directly bind the Background to a property which returns the respectively coloured brush. You will have to fire property change notifications in the setters of properties on which the colour is dependent.

e.g.

public string Name
{
    get { return _name; }
    set
    {
        if (_name != value)
        {
            _name = value;
            OnPropertyChanged("Name");
            OnPropertyChanged("NameBrush");
        }
    }
}

public Brush NameBrush
{
    get
    {
        switch (Name)
        {
            case "John":
                return Brushes.LightGreen;
            default:
                break;
        }

        return Brushes.Transparent;
    }
}

这篇关于根据值更改DataGrid单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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