简体   繁体   中英

Changing background color of the row in a grid

I'm working on a C#/WPF application.In one of the xaml screens, I've a MS windows datagrid and I'm binding my custom listview collection to it. This listview collection(ie MyCollection) contains prices for various products.The collection is of type MyProduct:

public class MyProduct
{
public Int32 Id {get;set;}
public string Name {get;set;}
public Decimal Price {get;set;} 
}

I need to change the background color of a row in the grid depending upon the price value. How do I achieve this please?

I thought I could do this using RowDataBound eventhandler but I don't see this eventhandler in the grid.

Set the background of DataGridRow in a style like this :

XAML :

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dataGrid" Margin="55,29,44,43" ItemsSource="{x:Static local:MainWindow.FakeList}">
            <DataGrid.Resources>
                <Style TargetType="DataGridRow">
                    <Setter Property="Background" Value="{Binding Price, Converter={x:Static local:MyPriceToBackgroundConverter.Instance}}"/>
                </Style>
            </DataGrid.Resources>
        </DataGrid>
    </Grid>
</Window>

Window class :

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public static List<MyProduct> FakeList
    {
        get
        {
            return new List<MyProduct>
            {
                new MyProduct { Price = 5 },
                new MyProduct { Price = 10 },
                new MyProduct { Price = 20 }
            };
        }
    }
}

Converter :

public class MyPriceToBackgroundConverter : IValueConverter
{
    private static MyPriceToBackgroundConverter instance;
    public static MyPriceToBackgroundConverter Instance
    {
        get
        {
            if (instance == null)
                instance = new MyPriceToBackgroundConverter();
            return instance;
        }
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        decimal price = (decimal)value;
        if (price > 8 && price < 12)
            return Brushes.Red;
        return Brushes.Azure;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Useless here
        throw new NotImplementedException();
    }
}

One way to do this is to implement InotifyPropertyChanged on MyProduct and add a property that contains the Brush you would like to color it in.

public class MyProduct : INotifyPropertyChanged
{
    protected int _Id;
    public int Id
    {
        get
        {
            return this._Id;
        }
        set
        {
            if (this._Id == value)
            {
                return;
            }
            this._Id = value;
            this.OnPropertyChanged();
        }
    }

    //... And so on
    protected decimal _Price;
    public decimal Price
    {
        get
        {
            return this._Price;
        }
        set
        {
            if (this._Price == value)
            {
                return;
            }
            this._Price = value;
            this.OnPropertyChanged();
            this.OnPropertyChanged("MyColor");
        }
    }

    public Brush MyColor
    {
        get
        {
            if( this._Price < 10)
            {
                return Brushes.Green;
            }
        }
        else
        {
            //And so on
        }

    }

    #region INPC
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string name = "")
    {
        PropertyChangedEventHandler tmp = this.PropertyChanged;
        if (tmp != null)
        {
            tmp(this, new PropertyChangedEventArgs(name));
        }
    }

    #endregion
}

And for your DataGrid do the following to bind the color to the background:

<DataGrid ...>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="{Binding MyColor}"/>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

Edit: JYL's solution is another way to do this andpossibly the better one as you don't need an extra property, but you need the converter. It comes down to preference, however I would suggest you go with his solution as I feel like it is cleaner and doesn't have UI stuff mixed into the class. Better separation of concerns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM