juergen d juergen d - 1 month ago 24
C# Question

Change WPF Datagrid Row Color

I have a WPF datagrid that is filled with an ObserverableCollection.

Now I want to color the rows depending on the row content at the program start and if something changes during runtime.

System.Windows.Controls.DataGrid areaDataGrid = ...;
ObservableCollection<Area> areas;
//adding items to areas collection
areaDataGrid.ItemsSource = areas;

areaDataGrid.Rows <-- Property not available. how to access rows here?

CollectionView myCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(areaDataGrid.Items);
((INotifyCollectionChanged)myCollectionView).CollectionChanged += new NotifyCollectionChangedEventHandler(areaDataGrid_Changed);
...

void areaDataGrid_Changed(object sender, NotifyCollectionChangedEventArgs e)
{
//how to access changed row here?
}


How can I access the rows at start and runtime?

Answer

Use the RowStyle. You can use the Triggers to change the color conditionally, or just bind it to a Brush property on your items and change that property respectively.

Comments