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 onAdd
ing toTodos
collection? If this is not the way to do this, then what is? - Why doesn't
OnTodosChanged
get called onAdd
ition of elements toTodos
collection? - 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
PropertyChanged
notification only occurs when theTodos
property 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
Count
property when items are added to theTodos
collection, then you need to subscribe to the collections'sCollectionChanged
event:I've also written a small blog series about the MVVM Source Generators, if you want to take a deeper dive into the topic.