Repository navigation
Description
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) 🔬
- Create a Skia Desktop app (or add this as a runtime test in
Uno.UI.RuntimeTests). - Load a
Buttoninto 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- Observe
button.ActualWidth == 0and no visual children. - 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.
uno/src/Uno.UI/UI/Xaml/Controls/Control/Control.cs
Lines 209 to 243 in e98c0dc
| 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
fix(control): Re-measure when Template changes