Repository navigation
fix: push templated parent's theme during template materialization - #23252
Conversation
When a FrameworkTemplate is materialized at runtime (DataGrid row
virtualization, ContentPresenter re-materialization, etc.) inside a
subtree whose RequestedTheme overrides the application theme, the
{ThemeResource} bindings in the template were resolving against the
application's theme rather than the subtree's theme. The wrong value
was captured on non-FrameworkElement DependencyObjects such as
attached behaviors, which never go through OnLoadingPartial themselves
and therefore had no re-resolution path.
Mirrors the push/pop pattern that FrameworkElement.OnLoadingPartial
already uses for FE loading. Push is skipped when the templated parent
is non-UIElement or its theme is Theme.None.
There was a problem hiding this comment.
Pull request overview
This PR fixes theme resource resolution during runtime template materialization so {ThemeResource} inside templates resolves against the templated parent’s effective subtree theme (e.g., a Light-themed subtree while the app theme is Dark), rather than always using the application theme. This addresses cases where non-FrameworkElement dependency objects (e.g., behaviors) capture the wrong themed value at materialization time and don’t get re-resolved until a later theme toggle.
Changes:
- Push the templated parent’s effective Light/Dark theme onto
ResourceDictionary’s subtree theme stack duringFrameworkTemplate.LoadContentmaterialization, and pop it afterward. - Add runtime tests covering manual
DataTemplate.LoadContent(templatedParent), end-to-endContentTemplatematerialization, and{ThemeResource}on a non-FEDependencyObjectinside a template.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Uno.UI/UI/Xaml/FrameworkTemplate.cs | Push/pop templated parent subtree theme around template factory invocation to ensure correct {ThemeResource} resolution during runtime materialization. |
| src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_ElementTheme.cs | Add regression/runtime tests validating themed subtree resolution for templates, including non-FE DependencyObject scenarios. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23252/wasm-skia-net9/index.html |
Summary
{ThemeResource}bindings inside a runtime-materialized template (e.g.DataGridrow virtualization,ContentPresenterre-materialization) resolved against the application theme rather than the templated parent's subtree theme.OnLoadingPartialre-resolution path of their own, so the wrong value captured at materialization time stuck around until the next app/element theme toggle.Themes.RequestedThemeForSubTreearound the factory invocation inFrameworkTemplate.LoadContent, mirroring the push/pop pattern thatFrameworkElement.OnLoadingPartialalready uses for FE loading. The push is skipped when the templated parent isn't aUIElementor its theme isTheme.None.Closes https://github.com/unoplatform/kahua-private/issues/475
Test plan
When_DataTemplate_LoadContent_Inside_Light_Subtree_Resolves_Lightis the sharpest repro: callsDataTemplate.LoadContentmanually from inside a Light subtree while the app theme is Dark, asserts{ThemeResource}resolves to the Light value. Fails onmasterwithout the fix; passes with the fix.When_ContentTemplate_Materialized_In_Themed_Subtree_Uses_Subtree_Themecovers theContentControl.ContentTemplateend-to-end flow.When_ThemeResource_On_NonFE_DependencyObject_Inside_Themed_Subtree_Resolves_Correctlycovers the non-FE DO inside aDataTemplate(matches the attached-behavior scenario in the linked issue).Given_ElementThemesuite (Skia Desktop): 98 passed / 9 failed both before and after the fix — the 9 failures are pre-existing test-ordering issues unrelated to this change (each passes in isolation).🤖 Generated with Claude Code