Skip to content

Use SetThemeResourceBinding for theme-aware resources set from code - #2259

Merged
Niels Laute (niels9001) merged 3 commits into
mainfrom
niels9001-set-theme-resource-binding
Oct 7, 2026
Merged

Niels Laute (niels9001) merged 3 commits into
mainfrom
niels9001-set-theme-resource-binding

Conversation

@niels9001

@niels9001 Niels Laute (niels9001) commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Adopts the experimental WinUI API FrameworkElement.SetThemeResourceBinding(DependencyProperty, string) from Windows App SDK 2.5.4-experimental. This API is the code equivalent of {ThemeResource Key}; see Theme resource binding API, RuntimeCompatibilityChange WinUI_SetThemeResourceBinding. No package update is needed because the repo already targets 2.5.4-experimental.

Bug fixes: two brushes were read once from Application.Current.Resources and assigned to a DependencyProperty, so they kept the old theme's value after a theme change:

  • PullToRefreshPage.xaml.cs: the BorderBrush of the ListView created in code now uses lv2.SetThemeResourceBinding(Control.BorderBrushProperty, "TextControlBorderBrush").
  • TableViewPage.xaml.cs (ApplyRowBanding): AlternatingRowBackground now uses SetThemeResourceBinding(TableView.AlternatingRowBackgroundProperty, "SubtleFillColorSecondaryBrush") when banding is on, and is set to null when it is off.

I scanned the other resource lookups in code-behind and left them unchanged because none assign a theme brush to a DP. They are Style lookups (InfoBadge, ContentDialog, Settings), Color values used for a RichEditBox text format, and the MainWindow DataTemplate.

New sample: the Resources page (XamlResources) gets a focused example, "Set a ThemeResource from code (experimental)". It shows two Borders side by side:

  • one set with a one-time Resources["Key"] lookup;
  • one set with SetThemeResourceBinding.

A "Switch theme" option flips the example's RequestedTheme, so you can watch only the bound Border update. The snippet is in XamlResourcesSetThemeResourceFromCode.txt. The "Theme resources" bullet list also gets a short mention of the API.

Data: added a SetThemeResourceBinding tag to the XamlResources entry in ControlInfoData.json; the existing flags are unchanged. Regenerated catalog/windows-samples.json.

Sibling PR: the DataTemplate(DataTemplateElementFactory) adoption is handled separately. This PR does not touch MenuItemTemplateSelector or the Templates page.

image
themeresource.mp4

Motivation and Context

Brushes assigned from code didn't follow theme changes. The new API fixes that and gives developers a gallery sample for this new experimental capability.

How Has This Been Tested?

  • msbuild WinUIGallery\WinUIGallery.csproj /p:Configuration=Debug /p:Platform=x64 /restore: succeeded, no new warnings.
  • msbuild WinUIGallery\WinUIGallery.csproj /p:Configuration=Debug /p:Platform=ARM64 /restore and /p:Configuration=Debug-Unpackaged /p:Platform=ARM64: succeeded.
  • dotnet run --project tools/CatalogExporter -- generate, then dotnet run --project tools/CatalogExporter -- check: passed; the index is up to date.
  • dotnet test tests\WinUIGallery.CatalogExporter.Tests: passed, 159/159.
  • Manual check on Windows ARM64 with the Debug-Unpackaged build: opened Fundamentals > Resources and found the new example. Clicked "Switch theme" twice. The SetThemeResourceBinding Border switched between Light and Dark accent colors each time, while the one-time lookup Border kept the original values.
  • Not run: UI tests and Axe tests (tests\WinUIGallery.UITests).

Screenshots (if appropriate):

After clicking "Switch theme" (example set to Dark): the left Border keeps the Light accent from the one-time lookup, and the right Border updates through SetThemeResourceBinding.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
- Fix PullToRefresh and TableView brushes read once from resources so they update on theme change.
- Add a 'Set a ThemeResource from code (experimental)' example to the Resources page.
- Add SetThemeResourceBinding tag and regenerate catalog.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@niels9001
Niels Laute (niels9001) merged commit adbf522 into main Oct 7, 2026
2 checks passed
@niels9001
Niels Laute (niels9001) deleted the niels9001-set-theme-resource-binding branch October 7, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants