Repository navigation
Use SetThemeResourceBinding for theme-aware resources set from code - #2259
Merged
Niels Laute (niels9001) merged 3 commits intoOct 7, 2026
Merged
Conversation
- 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>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Niels Laute (niels9001)
deleted the
niels9001-set-theme-resource-binding
branch
October 7, 2026 11:45
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, RuntimeCompatibilityChangeWinUI_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.Resourcesand assigned to a DependencyProperty, so they kept the old theme's value after a theme change:PullToRefreshPage.xaml.cs: theBorderBrushof the ListView created in code now useslv2.SetThemeResourceBinding(Control.BorderBrushProperty, "TextControlBorderBrush").TableViewPage.xaml.cs(ApplyRowBanding):AlternatingRowBackgroundnow usesSetThemeResourceBinding(TableView.AlternatingRowBackgroundProperty, "SubtleFillColorSecondaryBrush")when banding is on, and is set tonullwhen 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),
Colorvalues 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:Resources["Key"]lookup;SetThemeResourceBinding.A "Switch theme" option flips the example's
RequestedTheme, so you can watch only the bound Border update. The snippet is inXamlResourcesSetThemeResourceFromCode.txt. The "Theme resources" bullet list also gets a short mention of the API.Data: added a
SetThemeResourceBindingtag to the XamlResources entry inControlInfoData.json; the existing flags are unchanged. Regeneratedcatalog/windows-samples.json.Sibling PR: the
DataTemplate(DataTemplateElementFactory)adoption is handled separately. This PR does not touch MenuItemTemplateSelector or the Templates page.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 /restoreand/p:Configuration=Debug-Unpackaged /p:Platform=ARM64: succeeded.dotnet run --project tools/CatalogExporter -- generate, thendotnet run --project tools/CatalogExporter -- check: passed; the index is up to date.dotnet test tests\WinUIGallery.CatalogExporter.Tests: passed, 159/159.SetThemeResourceBindingBorder switched between Light and Dark accent colors each time, while the one-time lookup Border kept the original values.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