Change DataGrid cell colour based on values

前端 未结 8 1482
栀梦
栀梦 2020-11-22 08:48

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

8条回答
  •  太阳男子
    2020-11-22 09:09

    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:

    
        
            
        
    
    

    A Screenshot


    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:

    
        
    
    ...
    
        
            
        
    
    

    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(nameof(Name));
                OnPropertyChanged(nameof(NameBrush));
            }
        }
    }
    
    public Brush NameBrush
    {
        get
        {
            switch (Name)
            {
                case "John":
                    return Brushes.LightGreen;
                default:
                    break;
            }
    
            return Brushes.Transparent;
        }
    }
    

提交回复
热议问题