Skip to content

fix: push templated parent's theme during template materialization - #23252

Merged
MartinZikmund merged 1 commit into
masterfrom
dev/mazi/themingdg
May 13, 2026
Merged

MartinZikmund merged 1 commit into
masterfrom
dev/mazi/themingdg

Conversation

@MartinZikmund

Copy link
Copy Markdown
Member

Summary

  • {ThemeResource} bindings inside a runtime-materialized template (e.g. DataGrid row virtualization, ContentPresenter re-materialization) resolved against the application theme rather than the templated parent's subtree theme.
  • This was particularly visible for non-FrameworkElement DependencyObjects (e.g. attached behaviors): they have no OnLoadingPartial re-resolution path of their own, so the wrong value captured at materialization time stuck around until the next app/element theme toggle.
  • Fix: push the templated parent's effective theme onto Themes.RequestedThemeForSubTree around the factory invocation in FrameworkTemplate.LoadContent, mirroring the push/pop pattern that FrameworkElement.OnLoadingPartial already uses for FE loading. The push is skipped when the templated parent isn't a UIElement or its theme is Theme.None.

Closes https://github.com/unoplatform/kahua-private/issues/475

Test plan

  • New runtime test When_DataTemplate_LoadContent_Inside_Light_Subtree_Resolves_Light is the sharpest repro: calls DataTemplate.LoadContent manually from inside a Light subtree while the app theme is Dark, asserts {ThemeResource} resolves to the Light value. Fails on master without the fix; passes with the fix.
  • New runtime test When_ContentTemplate_Materialized_In_Themed_Subtree_Uses_Subtree_Theme covers the ContentControl.ContentTemplate end-to-end flow.
  • New runtime test When_ThemeResource_On_NonFE_DependencyObject_Inside_Themed_Subtree_Resolves_Correctly covers the non-FE DO inside a DataTemplate (matches the attached-behavior scenario in the linked issue).
  • Full Given_ElementTheme suite (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).
  • CI runtime tests across all platforms.

🤖 Generated with Claude Code

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.
Copilot AI review requested due to automatic review settings May 12, 2026 12:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 during FrameworkTemplate.LoadContent materialization, and pop it afterward.
  • Add runtime tests covering manual DataTemplate.LoadContent(templatedParent), end-to-end ContentTemplate materialization, and {ThemeResource} on a non-FE DependencyObject inside 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.

@unodevops

Copy link
Copy Markdown
Contributor

@MartinZikmund
MartinZikmund merged commit 40fec1f into master May 13, 2026
49 checks passed
@MartinZikmund
MartinZikmund deleted the dev/mazi/themingdg branch May 13, 2026 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants