Generated code tries to assign to a read only `ObservableCollection`
karmeye opened this issue · comments
Describe the bug
This code
[ObservableObject]
public partial class MyViewModel : ViewModel
{
public ObservableCollection<DataType> SelectedItems { get; } = [];
}
results in error:
CS0200 Property or indexer "cannot be assigned to" "it is read only"
in the owner of the MyViewModel
:
private void UpdateTwoWay_5_ItemsSource()
{
if (this.initialized)
{
if (this.dataRoot != null)
{
if (this.dataRoot.ViewModel != null)
{
if (this.dataRoot.ViewModel.MyViewModel!= null)
{
// Error here
this.dataRoot.ViewModel.MyViewModel.SelectedItems = (global::System.Collections.ObjectModel.ObservableCollection<global::Abc>)this.obj5.ItemsSource;
}
}
}
}
}
Regression
No response
Steps to reproduce
1. Do the above.
I'm using NuGet package: CommunityToolkit.Mvvm 8.2.2
Expected behavior
As I understand it, with collections you don't use, e.g., a List
marked with ObservableProperty
, you just do it the standard way with an ObservableCollection
, and therefore it is only assigned once. So it should work to not have a setter.
Screenshots
No response
IDE and version
VS 2022
IDE version
Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.9.2
Nuget packages
- CommunityToolkit.Common
- CommunityToolkit.Diagnostics
- CommunityToolkit.HighPerformance
- CommunityToolkit.Mvvm (aka MVVM Toolkit)
Nuget package version(s)
8.2.2
Additional context
Adding a setter seems to make everything work.
Help us help you
No, just wanted to report this
That code is generated by the XAML compiler, not by the MVVM Toolkit. The issue is it doesn't support init
only properties.