-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
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.