thedriveee
thedriveee

Reputation: 619

How to change row background color in DataGrid (WPF)?

I need to paint some row in DataGrid in some color.

I have created collection in main UI Thread:

ObservableCollection<SomeElement> col= new ObservableCollection<SomeElement> ();

Then I change it from other thread:

int someElementNumber = 1;
int someInputValue = 11;
col[someElementNumber].SomePropery = someInputValue;

I implemented INotifyPropertyChanged Interface on SomeElement so that my DataGrid update value in that row. But I want to check this value and depend on it print row in some background color:

if (someInputValue > 10) {
    //paint row in some color
}

Please give me advice how to do it. Thanks everybody for helping in advance!

Upvotes: 1

Views: 2875

Answers (1)

thedriveee
thedriveee

Reputation: 619

I found answer by myself. If somebody interest:

1) Make Binding with DataTrigger on CheckProperty in XAML. That property is not necessary to be visible.

<Window.Resources>
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <DataTrigger Binding="{Binding CheckProperty}" Value="Success">
                <Setter Property="Background" Value="Green" />
            </DataTrigger>
            <DataTrigger Binding="{Binding CheckProperty}" Value="Error">
                <Setter Property="Background" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

2) In thread, that update Collection col set CheckProperty some value depend on some condition.

int someElementNumber = 1;
int someInputValue = 11;
col[someElementNumber].SomePropery = someInputValue;
if (someInputValue > 10) {
    col[someElementNumber].CheckProperty = "Success";
}
else {
    col[someElementNumber].CheckProperty = "Error";
}

CheckProperty has to Rise Property changed event!

When that property just have been updated, DataTrigger will be invoked and it print current row in some background color depend on condition.

Upvotes: 1

Related Questions