❔ WPF DataGrid RowStyle DataTrigger - Doesn't Keep Default Theme

Good morning/afternoon/evening all, I am having some issues when setting a WPF DataGrid row background colour depending on a cell value. I am using ModernWPF (https://github.com/Kinnara/ModernWpf) for a Light/Dark theme, and Controlzex (https://github.com/ControlzEx/ControlzEx) as a theme manager to switch between the light and dark theme depending on the users Windows theme setting. My WPF DataGrid looks like this:
<DataGrid
AutoGenerateColumns="False"
ItemsSource="{Binding People}"
CanUserAddRows="False">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding FirstName}" Value="Alice">
<Setter
Property="Background" Value="LightSkyBlue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn
Header="First Name"
Binding="{Binding FirstName}" />
<DataGridTextColumn
Header="Last Name"
Binding="{Binding LastName}" />
<DataGridTextColumn
Header="Full Name"
Binding="{Binding FullName}" />
</DataGrid.Columns>
</DataGrid>
<DataGrid
AutoGenerateColumns="False"
ItemsSource="{Binding People}"
CanUserAddRows="False">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding FirstName}" Value="Alice">
<Setter
Property="Background" Value="LightSkyBlue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn
Header="First Name"
Binding="{Binding FirstName}" />
<DataGridTextColumn
Header="Last Name"
Binding="{Binding LastName}" />
<DataGridTextColumn
Header="Full Name"
Binding="{Binding FullName}" />
</DataGrid.Columns>
</DataGrid>
The trigger does set a the background colour to LightSkyBlue when the person's first name is Alice, however, it then sets all the background colours of the other rows to be white. This isn't an issue when using the Light theme, but when a user is on a Dark theme then the White background matches the White foreground text. Please see the attached comparison images where the Windows Light and Dark theme are used. For further assistance, please find attached a copy of a small self contained example of the issue: DataGridUI.zip. Please may someone help me in setting the row style, without affecting the default style.
GitHub
GitHub - Kinnara/ModernWpf: Modern styles and controls for your WPF...
Modern styles and controls for your WPF applications - GitHub - Kinnara/ModernWpf: Modern styles and controls for your WPF applications
GitHub
GitHub - ControlzEx/ControlzEx: Shared Controlz for WPF and ... more
Shared Controlz for WPF and ... more. Contribute to ControlzEx/ControlzEx development by creating an account on GitHub.
8 Replies
Nullable Truth
Nullable TruthOP14mo ago
Of course if my way of doing this is wrong then I would like to know too.
Klarth
Klarth14mo ago
What if you try BasedOn? eg. <Style TargetType="DataGridRow" BasedOn="{StaticResource DefaultDataGridRowStyle}">
nohopestage
nohopestage14mo ago
^ beat me to it
Nullable Truth
Nullable TruthOP14mo ago
I believe I tried the 'BasedOn' attribute to no avail Oh my days, I was setting the based on to just 'DataGridRow' This works exactly as I need it to.
Nullable Truth
Nullable TruthOP14mo ago
How did you know which static resource name to use?
No description
Klarth
Klarth14mo ago
I searched the repo for DataGridRow and found the style.
Nullable Truth
Nullable TruthOP14mo ago
Ahh, I may have to do some digging around for other areas of my application. Thank you very much to both of you for looking into this.
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server