Description
When a Style object is mutated in-place (e.g., a Setter's Value is changed), the framework does not reapply it to the target element. This is because MergedStyle uses reference equality to decide whether to unapply/reapply:
// MergedStyle.cs line 48
if (_style == value) return;
// MergedStyle.cs line 190
bool shouldReApplyStyle = implicitStyle != ImplicitStyle || classStyles != ClassStyles || Style != style;
Making Style fully observable would be expensive (change tracking on Setters collection and each Setter's properties). A cheaper alternative would be to expose an API that forces reapplication of the current style, e.g.:
// Option A: on the element
element.InvalidateStyle();
// Option B: on MergedStyle (internal, for framework use)
_mergedStyle.Reapply();
This would unapply and reapply the current Style, ClassStyles, and ImplicitStyle without requiring a new object reference.
Motivation
Hot Reload scenarios (including XAML Incremental Hot Reload) need to update Style setters in-place and have the changes reflected on the element. Currently the only workaround is to create a new Style instance and reassign it, which is wasteful for styles with many setters or deep BasedOn chains.
Proposed API
public abstract class StyleableElement : Element
{
// Forces unapply + reapply of the current merged style
public void InvalidateStyle();
}
Or alternatively, an internal-only API if this should not be public.
Description
When a
Styleobject is mutated in-place (e.g., a Setter'sValueis changed), the framework does not reapply it to the target element. This is becauseMergedStyleuses reference equality to decide whether to unapply/reapply:Making
Stylefully observable would be expensive (change tracking on Setters collection and each Setter's properties). A cheaper alternative would be to expose an API that forces reapplication of the current style, e.g.:This would unapply and reapply the current
Style,ClassStyles, andImplicitStylewithout requiring a new object reference.Motivation
Hot Reload scenarios (including XAML Incremental Hot Reload) need to update Style setters in-place and have the changes reflected on the element. Currently the only workaround is to create a new Style instance and reassign it, which is wasteful for styles with many setters or deep
BasedOnchains.Proposed API
Or alternatively, an internal-only API if this should not be public.