Skip to content

When resolving XAML C# expression bindings, prefer Instance members over Static members #34473

@egvijayanand

Description

@egvijayanand

Description

When resolving XAML C# expression bindings, prefer Instance members rather than Static members.

Context:

Most of the time, the type name and the property name are the same.

In such cases, XAML C# expression bindings try to resolve the ViewModel property as a static type member, especially when nested properties are involved.

Example:

public partial class Event : ObservableObject
{
    [ObservableProperty]
    public partial string Notes { get; set; }
}

public partial class EventViewModel : ObservableObject
{
    public Event Event { get; } = new();
}

This tries to resolve the static member Notes in the Event type, but it’s actually an instance member, making it a nested property access.

<Entry Text="{Event.Notes}" />

In most cases, we bind to instance members, and using static members is uncommon.

For a static member, there's also an alternative option called x:Static.

Since a static member is preferred over an instance member, it requires different handling, which can be inconvenient. Retain the current behavior as it is.

Public API Changes

Prefer Instance members rather than Static members.

Intended Use-Case

Identical type and property name used with nested properties.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions