List<> collection does not update the View in MVVM

前端 未结 4 967
闹比i
闹比i 2021-01-15 12:36

I used the List collection as the ItemsSource for the DataGrid control.

But it did not update the View if i remove the item from the List

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-15 13:06

    ObservableCollection fires change events every time you change the items in the collection. List doesn't. That's the reason.

    DataBinding is lazy. If you don't tell your view that something has changed, it won't bother updating. Under the hoods, WPF DataBinding registers for change notifications, so that your ViewModel can tell the view when it has changed. It does this with interfaces like INotifyPropertyChanged and INotifyCollectionChanged.

    ObservableCollection implements the interface INotifyCollectionChanged. This interface defines the event CollectionChanged, which your View basically attaches its own event handler to. That handler will update the view when the event is raised by the collection.

提交回复
热议问题