I found quite a few questions/answers about this here on SO but I'm confused about something. Here's one example: Observablecollection not updating list, when an item gets added
Shouldn't adding a new item to an ObservableCollection<T>
automatically trigger a UI update? A lot of people are suggesting solutions that indicate developer should manually call NotifyPropertyChanged()
but if that's the case, why use an ObservableCollection
in the first place? Again, I'm specifically talking about adding a new item to the ObservableCollection
and NOT updating a property of an existing item.
Using the following code, I add a brand new item to my ObservableCollection<Student>
and there's no change in the UI even though the new item is added to the ObservableCollection
.
Here's my view model code:
public partial Class MyViewModel : ObservableObject
{
//...
public ObservableCollection<Student> Students { get; } = new();
[RelayCommand]
void AddStudent(Student student)
{
Students.Add(student);
}
}
And my CollectionView
binds to Students
like so:
<CollectionView
ItemsSource={Binding Students}>
<!-- Omitted for brevity -->
</CollectionView>