Skip to content

Commit

Permalink
Fixes DataGrid colors not updating on theme switch (#4138)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
msftbot[bot] authored Aug 5, 2021
2 parents 794b295 + 13f40bb commit ee93385
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 38 deletions.
18 changes: 13 additions & 5 deletions Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<StaticResource x:Key="DataGridColumnHeaderDraggedBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumLowBrush"/>
<StaticResource x:Key="DataGridColumnHeaderPointerOverBrush" ResourceKey="SystemControlHighlightListLowBrush"/>
<StaticResource x:Key="DataGridColumnHeaderPressedBrush" ResourceKey="SystemControlHighlightListMediumBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderPressedBackgroundBrush" ResourceKey="SystemControlBackgroundListMediumBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderForegroundBrush" ResourceKey="SystemControlForegroundBaseHighBrush"/>
<StaticResource x:Key="DataGridRowHoveredBackgroundColor" ResourceKey="SystemListLowColor"/>
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<SolidColorBrush x:Key="InvalidBrush" Color="#FFFF00"/>
Expand All @@ -41,6 +45,10 @@
<StaticResource x:Key="DataGridColumnHeaderDraggedBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumLowBrush"/>
<StaticResource x:Key="DataGridColumnHeaderPointerOverBrush" ResourceKey="SystemControlHighlightListLowBrush"/>
<StaticResource x:Key="DataGridColumnHeaderPressedBrush" ResourceKey="SystemControlHighlightListMediumBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderPressedBackgroundBrush" ResourceKey="SystemControlBackgroundListMediumBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderForegroundBrush" ResourceKey="SystemControlForegroundBaseHighBrush"/>
<StaticResource x:Key="DataGridRowHoveredBackgroundColor" ResourceKey="SystemListLowColor"/>
</ResourceDictionary>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="InvalidBrush" Color="#C50500"/>
Expand All @@ -54,6 +62,10 @@
<StaticResource x:Key="DataGridColumnHeaderDraggedBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumLowBrush"/>
<StaticResource x:Key="DataGridColumnHeaderPointerOverBrush" ResourceKey="SystemControlHighlightListLowBrush"/>
<StaticResource x:Key="DataGridColumnHeaderPressedBrush" ResourceKey="SystemControlHighlightListMediumBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderPressedBackgroundBrush" ResourceKey="SystemControlBackgroundListMediumBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderForegroundBrush" ResourceKey="SystemControlForegroundBaseHighBrush"/>
<StaticResource x:Key="DataGridRowHoveredBackgroundColor" ResourceKey="SystemListLowColor"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

Expand Down Expand Up @@ -89,13 +101,9 @@
<StaticResource x:Key="DataGridRowSelectedUnfocusedBackgroundOpacity" ResourceKey="ListAccentLowOpacity"/>
<StaticResource x:Key="DataGridRowSelectedHoveredUnfocusedBackgroundColor" ResourceKey="SystemAccentColor"/>
<StaticResource x:Key="DataGridRowSelectedHoveredUnfocusedBackgroundOpacity" ResourceKey="ListAccentMediumOpacity"/>
<StaticResource x:Key="DataGridRowHoveredBackgroundColor" ResourceKey="SystemListLowColor"/>
<StaticResource x:Key="DataGridRowHeaderForegroundBrush" ResourceKey="SystemControlForegroundBaseMediumBrush"/>
<StaticResource x:Key="DataGridRowHeaderBackgroundBrush" ResourceKey="SystemControlBackgroundAltHighBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderBackgroundBrush" ResourceKey="SystemControlBackgroundChromeMediumBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderHoveredBackgroundBrush" ResourceKey="SystemControlBackgroundListLowBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderPressedBackgroundBrush" ResourceKey="SystemControlBackgroundListMediumBrush"/>
<StaticResource x:Key="DataGridRowGroupHeaderForegroundBrush" ResourceKey="SystemControlForegroundBaseHighBrush"/>
<StaticResource x:Key="DataGridRowInvalidBrush" ResourceKey="InvalidBrush"/>
<StaticResource x:Key="DataGridCellBackgroundBrush" ResourceKey="SystemControlTransparentBrush"/>
<StaticResource x:Key="DataGridCellFocusVisualPrimaryBrush" ResourceKey="SystemControlFocusVisualPrimaryBrush"/>
Expand Down Expand Up @@ -967,4 +975,4 @@
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</ResourceDictionary>
37 changes: 4 additions & 33 deletions Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,40 +1122,11 @@ internal void EnsureForeground()
{
DiagnosticsDebug.Assert(this.Index != -1, "Expected Index other than -1.");

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

if (this.Foreground.Equals(defaultForeground))
{
if (this.Index % 2 == 0 || this.OwningGrid.AlternatingRowForeground == null)
{
// Use OwningGrid.RowForeground if the index is even or if the OwningGrid.AlternatingRowForeground is null
if (this.OwningGrid.RowForeground != null)
{
newForeground = this.OwningGrid.RowForeground;
}
}
else
{
// Alternate row
if (this.OwningGrid.AlternatingRowForeground != null)
{
newForeground = this.OwningGrid.AlternatingRowForeground;
}
}

if (newForeground == null)
{
newForeground = this.Foreground;
}
}
else
{
newForeground = this.Foreground;
}

this.ComputedForeground = newForeground;
this.ComputedForeground = newForeground ?? this.Foreground;
}
else
{
Expand Down

0 comments on commit ee93385

Please sign in to comment.