In my XAML code, I want to set the Background
color of each row, based on a value of the object in one specific row. I have an ObservableCollection
of z
, and each of the z
has a property called State
. I started out with something like this in my DataGrid
:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background"
Value="{Binding z.StateId, Converter={StaticResource StateIdToColorConverter}}"/>
</Style>
</DataGrid.RowStyle>
This is a wrong approach because x is not a property in my ViewModel class.
In my ViewModel class I have an ObservableCollection<z>
which is the ItemsSource
of this DataGrid
, and a SelectedItem
of type z
.
I could bind the color to SelectedItem
, but this will only change one row in the DataGrid
.
How can I, based on one property change this rows backgroundcolor?
Use a
DataTrigger
:The same can be done without
DataTrigger
too:Where
BooleanToBrushConverter
is the following class:In XAML, add and define a RowStyle Property for the DataGrid with a goal to set the Background of the Row, to the Color defined in my Employee Object.
And in my Employee Class
This way every Row of the DataGrid has the BackGround Color of the
ColorSet
Property of my Object.