I would expect something like this to work:
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Count))]
public ObservableCollection<TODO> todos = new();
public int Count => Todos.Count;
While the Count is getting updated at the ViewModel layer, the UI does not seem to update. I have also tried using the OnTodosChanged method generated by the Mvvm CommunityToolkit source generator but that is not getting fired as well. Note that I am expected the UI to update on invoking the Add, etc methods of Todos.
- Why doesn't
[NotifyPropertyChangedFor(nameof(Count))]update the UI Count onAdding toTodoscollection? If this is not the way to do this, then what is? - Why doesn't
OnTodosChangedget called onAddition of elements toTodoscollection? - Would the UI update for an operation like
Todos[0].IsCompleted = true? If not, what is the way to handle this?
The reason I decided to post here was I could not find any relevant documentation (which is quite frustrating because I am thinking this might be something that is commonly done) nor any tutorial videos / sample apps leading me to question if this is the correct way to go about this. Help would be appreciated! - Thank you :^)
What you're doing with the MVVM Source Generators here:
is equivalent to writing the following:
The
PropertyChangednotification only occurs when theTodosproperty is changed, but not when the content of theObservableCollection<TODOS>changes. Adding elements to the collection is not changing the property.If you want to notify changes to the
Countproperty when items are added to theTodoscollection, then you need to subscribe to the collections'sCollectionChangedevent:I've also written a small blog series about the MVVM Source Generators, if you want to take a deeper dive into the topic.