Skip to content

Commit ee93385

Browse files
author
msftbot[bot]
authored
Fixes DataGrid colors not updating on theme switch (#4138)
## Fixes #4119 Fixes DataGrid colors not updating on theme switch. ## PR Type What kind of change does this PR introduce? - Bugfix ## What is the current behavior? Some DataGrid row group header brushes do not update upon theme switching, resulting in a state where rows are in a theme and row group headers are in the opposite theme. This behavior is undesirable. ## What is the new behavior? In this update the related brushes were moved into the ThemeDictionaries. After these changes, the DataGrid as a whole switches from a theme to another. ## PR Checklist Please check if your PR fulfills the following requirements: - [ ] Tested code with current [supported SDKs](../readme.md#supported) - [ ] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link --> - [ ] Sample in sample app has been added / updated (for bug fixes / features) - [ ] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/CommunityToolkit/WindowsCommunityToolkit-design-assets) - [ ] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/CommunityToolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc... - [ ] Tests for the changes have been added (for bug fixes / features) (if applicable) - [ ] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [X] Contains **NO** breaking changes ## Other information Tested in Windows 21H1 only.
2 parents 794b295 + 13f40bb commit ee93385

File tree

2 files changed

+17
-38
lines changed

2 files changed

+17
-38
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.xaml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
<StaticResource x:Key="DataGridColumnHeaderDraggedBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumLowBrush"/>
2929
<StaticResource x:Key="DataGridColumnHeaderPointerOverBrush" ResourceKey="SystemControlHighlightListLowBrush"/>
3030
<StaticResource x:Key="DataGridColumnHeaderPressedBrush" ResourceKey="SystemControlHighlightListMediumBrush"/>
31+
<StaticResource x:Key="DataGridRowGroupHeaderBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumBrush"/>
32+
<StaticResource x:Key="DataGridRowGroupHeaderPressedBackgroundBrush" ResourceKey="SystemControlBackgroundListMediumBrush"/>
33+
<StaticResource x:Key="DataGridRowGroupHeaderForegroundBrush" ResourceKey="SystemControlForegroundBaseHighBrush"/>
34+
<StaticResource x:Key="DataGridRowHoveredBackgroundColor" ResourceKey="SystemListLowColor"/>
3135
</ResourceDictionary>
3236
<ResourceDictionary x:Key="HighContrast">
3337
<SolidColorBrush x:Key="InvalidBrush" Color="#FFFF00"/>
@@ -41,6 +45,10 @@
4145
<StaticResource x:Key="DataGridColumnHeaderDraggedBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumLowBrush"/>
4246
<StaticResource x:Key="DataGridColumnHeaderPointerOverBrush" ResourceKey="SystemControlHighlightListLowBrush"/>
4347
<StaticResource x:Key="DataGridColumnHeaderPressedBrush" ResourceKey="SystemControlHighlightListMediumBrush"/>
48+
<StaticResource x:Key="DataGridRowGroupHeaderBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumBrush"/>
49+
<StaticResource x:Key="DataGridRowGroupHeaderPressedBackgroundBrush" ResourceKey="SystemControlBackgroundListMediumBrush"/>
50+
<StaticResource x:Key="DataGridRowGroupHeaderForegroundBrush" ResourceKey="SystemControlForegroundBaseHighBrush"/>
51+
<StaticResource x:Key="DataGridRowHoveredBackgroundColor" ResourceKey="SystemListLowColor"/>
4452
</ResourceDictionary>
4553
<ResourceDictionary x:Key="Light">
4654
<SolidColorBrush x:Key="InvalidBrush" Color="#C50500"/>
@@ -54,6 +62,10 @@
5462
<StaticResource x:Key="DataGridColumnHeaderDraggedBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumLowBrush"/>
5563
<StaticResource x:Key="DataGridColumnHeaderPointerOverBrush" ResourceKey="SystemControlHighlightListLowBrush"/>
5664
<StaticResource x:Key="DataGridColumnHeaderPressedBrush" ResourceKey="SystemControlHighlightListMediumBrush"/>
65+
<StaticResource x:Key="DataGridRowGroupHeaderBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumBrush"/>
66+
<StaticResource x:Key="DataGridRowGroupHeaderPressedBackgroundBrush" ResourceKey="SystemControlBackgroundListMediumBrush"/>
67+
<StaticResource x:Key="DataGridRowGroupHeaderForegroundBrush" ResourceKey="SystemControlForegroundBaseHighBrush"/>
68+
<StaticResource x:Key="DataGridRowHoveredBackgroundColor" ResourceKey="SystemListLowColor"/>
5769
</ResourceDictionary>
5870
</ResourceDictionary.ThemeDictionaries>
5971

@@ -89,13 +101,9 @@
89101
<StaticResource x:Key="DataGridRowSelectedUnfocusedBackgroundOpacity" ResourceKey="ListAccentLowOpacity"/>
90102
<StaticResource x:Key="DataGridRowSelectedHoveredUnfocusedBackgroundColor" ResourceKey="SystemAccentColor"/>
91103
<StaticResource x:Key="DataGridRowSelectedHoveredUnfocusedBackgroundOpacity" ResourceKey="ListAccentMediumOpacity"/>
92-
<StaticResource x:Key="DataGridRowHoveredBackgroundColor" ResourceKey="SystemListLowColor"/>
93104
<StaticResource x:Key="DataGridRowHeaderForegroundBrush" ResourceKey="SystemControlForegroundBaseMediumBrush"/>
94105
<StaticResource x:Key="DataGridRowHeaderBackgroundBrush" ResourceKey="SystemControlBackgroundAltHighBrush"/>
95-
<StaticResource x:Key="DataGridRowGroupHeaderBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumBrush"/>
96106
<StaticResource x:Key="DataGridRowGroupHeaderHoveredBackgroundBrush" ResourceKey="SystemControlBackgroundListLowBrush"/>
97-
<StaticResource x:Key="DataGridRowGroupHeaderPressedBackgroundBrush" ResourceKey="SystemControlBackgroundListMediumBrush"/>
98-
<StaticResource x:Key="DataGridRowGroupHeaderForegroundBrush" ResourceKey="SystemControlForegroundBaseHighBrush"/>
99107
<StaticResource x:Key="DataGridRowInvalidBrush" ResourceKey="InvalidBrush"/>
100108
<StaticResource x:Key="DataGridCellBackgroundBrush" ResourceKey="SystemControlTransparentBrush"/>
101109
<StaticResource x:Key="DataGridCellFocusVisualPrimaryBrush" ResourceKey="SystemControlFocusVisualPrimaryBrush"/>
@@ -967,4 +975,4 @@
967975
</Setter.Value>
968976
</Setter>
969977
</Style>
970-
</ResourceDictionary>
978+
</ResourceDictionary>

Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridRow.cs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,40 +1122,11 @@ internal void EnsureForeground()
11221122
{
11231123
DiagnosticsDebug.Assert(this.Index != -1, "Expected Index other than -1.");
11241124

1125-
PropertyMetadata metadataInfo = DataGridRow.ForegroundProperty.GetMetadata(typeof(DataGridRow));
1126-
Brush defaultForeground = metadataInfo == null ? null : metadataInfo.DefaultValue as Brush;
1127-
Brush newForeground = null;
1125+
var newForeground = this.Index % 2 == 0 || this.OwningGrid.AlternatingRowForeground == null
1126+
? this.OwningGrid.RowForeground
1127+
: this.OwningGrid.AlternatingRowForeground;
11281128

1129-
if (this.Foreground.Equals(defaultForeground))
1130-
{
1131-
if (this.Index % 2 == 0 || this.OwningGrid.AlternatingRowForeground == null)
1132-
{
1133-
// Use OwningGrid.RowForeground if the index is even or if the OwningGrid.AlternatingRowForeground is null
1134-
if (this.OwningGrid.RowForeground != null)
1135-
{
1136-
newForeground = this.OwningGrid.RowForeground;
1137-
}
1138-
}
1139-
else
1140-
{
1141-
// Alternate row
1142-
if (this.OwningGrid.AlternatingRowForeground != null)
1143-
{
1144-
newForeground = this.OwningGrid.AlternatingRowForeground;
1145-
}
1146-
}
1147-
1148-
if (newForeground == null)
1149-
{
1150-
newForeground = this.Foreground;
1151-
}
1152-
}
1153-
else
1154-
{
1155-
newForeground = this.Foreground;
1156-
}
1157-
1158-
this.ComputedForeground = newForeground;
1129+
this.ComputedForeground = newForeground ?? this.Foreground;
11591130
}
11601131
else
11611132
{

0 commit comments

Comments
 (0)