Nishant
Nishant

Reputation: 109

How to change background color of row in datagrid in wpf?

I want to change the background of row in datagrid if the value of a row item exceeds a certain value.

Upvotes: 0

Views: 981

Answers (2)

Fede
Fede

Reputation: 44028

This is the way to do it without resorting to procedural code, directly in XAML, which is where styling belongs.

Upvotes: 2

Eirik
Eirik

Reputation: 4205

You can do this in the LoadingRow event, like so:

private void dataGridLoadingRow(object sender, DataGridRowEventArgs e)
{
    YourObject rowContext = e.Row.DataContext as YourObject;

    if (rowContext != null)
    {
        if (rowContext.YourValue > _someValue)
            e.Row.Background = new SolidColorBrush(Colors.Green);
    }
}

Upvotes: 0

Related Questions