Skip to content

XAML C# expression tries to assign a value to the read-only properties, but it should be based on the binding mode #34075

@egvijayanand

Description

@egvijayanand

Description

XAML C# expression tries to assign a value to the read-only properties, but it should be based on the binding mode.

Try binding a ViewModel's Command property to the Button or a getter-only property to the Label.

Command:

private RelayCommand? incrementCommand;
public IRelayCommand IncrementCommand => incrementCommand ??= new RelayCommand(new Action(Increment));

[ObservableProperty]
public partial string CountText { get; private set; } = "Current count: 0";

XAML:

<Button Command="{IncrementCommand}" />
<Label Text="{CountText}" />

But ends up in a build error.

Error message: Property or indexer 'MainViewModel.IncrementCommand' cannot be assigned to -- it is read only.

It seems that it tries to assign a value to the read-only Command property. In general, command properties are intended to be read-only, featuring only a getter.

Steps to Reproduce

Try binding a getter-only, like Command, property in XAML using a C# expression.

Link to public reproduction project repository

Easy to reproduce.

Version with bug

10.0.40

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

All

Did you find any workaround?

Revert to the classic Binding extension.

Relevant log output

The Property or indexer 'MainViewModel.IncrementCommand' cannot be assigned to -- it is read only.
The property or indexer 'MainViewModel.CountText' cannot be used in this context because the set accessor is inaccessible.

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions