Kage
Kage

Reputation: 563

BindableCollection and NotifyOfPropertyChange

While messing around with Caliburn Micro, I had a DataGrid named MyCollection, which uses conventions to set the Itemsource to a collection named MyCollection in my viewmodel. I declared it like this:

public BindableCollection<ModelClass> MyCollection{ get; set; }

When I run, I get no items, so I change it to

 public BindableCollection<ModelClass> MyCollection
    {
        get => _myCollection;
        set
        {
            if (Equals(value, _myCollection)) return;
            _myCollection= value;
            NotifyOfPropertyChange(() => MyCollection);
        }
    }

and it works. I had noticed that BindableCollection has an IsNotifying property, and it's a subclass of ObservableCollection. I thought this meant it would fire the NotifyOfPropertyChange's automatically, so I figure I'm misunderstanding the purpose of these collections.

Can someone help me understand?

Upvotes: 0

Views: 1959

Answers (1)

mm8
mm8

Reputation: 169270

An ObservableCollection<T> provides notifications to the UI when items get added or removed from the collection and when the whole collection is refreshed.

It does however not notify the view when the MyCollection property is set to a new collection. So if you are assigning the MyCollection property dynamically after the view has been loaded, you need to raise the PropertyChanged event for the MyCollection source property for the view to be reloaded with the new collection.

Upvotes: 1

Related Questions