Skip to content

Add API to force reapplication of an element's Style #34721

@StephaneDelcroix

Description

@StephaneDelcroix

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    s/triagedIssue has been reviewed

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions