1

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>
7
  • Depends on when Students is initialized and the viewmodel applied as binding context for this view. Your property should include a setter and an OnPropertyChanged call in order for the view to be notified of someone setting a new collection to this property Commented Nov 20, 2023 at 6:55
  • 1
    It would be helpful to see some more relevant code, but setting the BindingContext in the constructor of your page sounds right. Note: Setting the x:DataType is not equivalent to setting the BindingContext. The x:DataType serves 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. Commented Nov 20, 2023 at 8:01
  • 1
    If you don't use the MVVM Toolkit does it work then? Would be weird, but just trying to narrow down causes. This should just work. Also are you in .NET 7? 8? Does this happen only on a single platform or all? I guess a small project showing this issue would be helpful Commented Nov 20, 2023 at 13:05
  • 2
    Just to consider all the options, could it be possible that the UI is being updated, but the CollectionView doesn't have an Height, so it seems that nothing is happening? I have this doubt because at the beginning the ObservableCollection is empty. Could you try initializing it with one or two students? Or setting manually the Height property of CollectionView? Or even ensure that the CollectionView is inside an expanded layout, like a Grid? Commented Nov 20, 2023 at 16:16
  • 1
    @RiccardoMinato Thank you! This feels like a good time to eat some humble pie and publicly admit the stupid mistake I was making. You were partially right. It wasn't the height of the CollectionView but the IsVisible setting! Thank you all for chiming in with your input. I appreciate it! Commented Nov 20, 2023 at 19:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.