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>
BindingContextin the constructor of your page sounds right. Note: Setting thex:DataTypeis not equivalent to setting theBindingContext. Thex:DataTypeserves two purposes: 1. Allowing the designer to evaluate bindings in the IDE, 2. enable compiled bindings to evaluate binding expressions during compile time and boost binding performance during runtime.CollectionViewdoesn't have anHeight, so it seems that nothing is happening? I have this doubt because at the beginning theObservableCollectionis empty. Could you try initializing it with one or two students? Or setting manually theHeightproperty ofCollectionView? Or even ensure that theCollectionViewis inside an expanded layout, like aGrid?CollectionViewbut theIsVisiblesetting! Thank you all for chiming in with your input. I appreciate it!