Skip to content

Setting Control.Template on a loaded control never applies the new template (Control.Template is missing AffectsMeasure) #25217

Done

Description

@MartinZikmund

Current behavior 🐛

Setting Control.Template on a control that is already loaded never applies the new template. OnTemplateChanged removes the old template root, but nothing schedules a new measure pass, so ApplyTemplate (which instantiates the template) never runs. The control stays with no visual children and ActualWidth == 0 until something else invalidates its measure.

For example, button.Template = null; (idle) button.Template = originalTemplate; (idle) leaves the Button blank (ActualWidth goes 52 -> 0 and stays 0). Calling button.InvalidateMeasure() after the assignment restores it.

Expected behavior 🎯

Like WinUI, assigning a new Template should re-measure the control and apply the new template. In WinUI, Control.Template is registered with the AffectMeasure metadata flag (StaticMetadata.g.cpp, KnownPropertyIndex::Control_Template). This was checked in the WinUI C++ sources only; I did not run a native WinUI repro.

How to reproduce it (as minimally and precisely as possible) 🔬

  1. Create a Skia Desktop app (or add this as a runtime test in Uno.UI.RuntimeTests).
  2. Load a Button into the window and wait for layout, then swap the template:
var button = new Button { Content = "Hello" };
await UITestHelper.Load(button);          // or: Content = button; await idle

var template = button.Template;
var initialWidth = button.ActualWidth;    // 52

button.Template = null;
await WindowHelper.WaitForIdle();
button.Template = template;
await WindowHelper.WaitForIdle();

Assert.AreEqual(initialWidth, button.ActualWidth); // fails: actual is 0
  1. Observe button.ActualWidth == 0 and no visual children.
  2. Add button.InvalidateMeasure(); after the second assignment and the button renders again (52).

Workaround 🛠️

Call InvalidateMeasure() on the control after changing Template.

Works on UWP/WinUI

Yes (by WinUI source: Control.Template has AffectMeasure).

Environment

Windows (Skia Desktop / Win32)

Uno.Sdk version (and other relevant versions) 📦

Uno master at e98c0dc (7.0 dev), net11.0-desktop, .NET SDK 11.0.100-rc.1.

Affected platforms

Verified at runtime on Skia Desktop (Win32). The code involved is shared, so other Skia heads are likely affected; I did not test them.

Anything else we need to know? 💬

Root cause, by code inspection: the TemplateProperty metadata in Control.cs drops WinUI's measure flag, with a comment saying it is applied "conditionally in SetUpdateControlTemplate". That method no longer exists, so the flag is never applied.

public static DependencyProperty TemplateProperty { get; } =
DependencyProperty.Register(
nameof(Template),
typeof(ControlTemplate),
typeof(Control),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.ValueDoesNotInheritDataContext, // WinUI also has AffectsMeasure here, but we only do this conditionally in SetUpdateControlTemplate.
(s, e) => ((Control)s)?.OnTemplateChanged(e)));
private protected virtual void OnTemplateChanged(DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue)
{
// Reset the template bindings for this control
//ClearPropertySubscriptions();
// When the control template property is set, we clear the visual children
var pUIElement = this.GetFirstChild();
if (pUIElement is { })
{
//CFrameworkTemplate* pNewTemplate = NULL;
//if (e.NewValue?.GetType() == valueObject)
//{
// IFC(DoPointerCast(pNewTemplate, args.m_value.AsObject()));
//}
//else if (args.m_value.GetType() != valueNull)
//{
// IFC(E_INVALIDARG);
//}
RemoveChild(pUIElement);
//IFC(GetContext()->RemoveNameScope(this, Jupiter::NameScoping::NameScopeType::TemplateNameScope));
}
}
}

new FrameworkPropertyMetadata(
    null,
    FrameworkPropertyMetadataOptions.ValueDoesNotInheritDataContext, // WinUI also has AffectsMeasure here, but we only do this conditionally in SetUpdateControlTemplate.
    (s, e) => ((Control)s)?.OnTemplateChanged(e)));

OnTemplateChanged only calls RemoveChild on the old template root.

A possible fix is to add FrameworkPropertyMetadataOptions.AffectsMeasure here, or call InvalidateMeasure() from OnTemplateChanged.

Impact: any runtime template swap (restyling, theme/style switchers, anything that re-applies templates on a loaded control) leaves the control blank until something else invalidates its measure.

Activity

  1. added
    kind/bug
    triage/untriaged
    area/skia ✏️
    project/styling 👔
    project/layout 🧱
    difficulty/starter 🚀
    7.1
    on Oct 11, 2026
  2. removed
    7.1
    on Oct 29, 2026
  3. added a commit that references this issue on Nov 24, 2026
    00b09c1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/skia ✏️difficulty/starter 🚀kind/bugproject/layout 🧱project/styling 👔triage/untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions