Binding List and UI controls, not updating on edit

2019-07-19 03:29发布

问题:

I am binding a BindingList two way to a listbox. The Binding list contains a number of images which apparently only update the listbox if items are added or removed from the binding list. How can I make it so that the bindinglist also raises the listchanged event when an item is modified?

EDIT: I find the problem I am having is that a property of an object is not being changed, rather the base object.

BindingList<ImageSource>();

This wont work however if I did this:

BindingList<Image>();

And then set the binding path to Image.Source, it would update correctly and this is because a property of the Image has changed but in the case of the first example, only a direct item in the list has changed. So how may I get the same behaviour as the second example?

FINAL EDIT : It seems that using ObservableCollection instead of BindingList fixes this issue. I was under the impression that they were identical in notifying of changes in the collection. Full answer below

回答1:

The list does raise that event but only if the underlying items provides the proper notifications via INotifyPropertyChanged.



回答2:

The BindingList differs from ObservableCollection in that BindingList does not notify that its direct items are changed (except when items are added or removed from the collection). ObservableCollection however implements INotifyCollectionChanged and INotifyPropertyChanged interfaces. This means that any change to direct items of an ObservableCollection are reported to the UI.

If you are using bindings to direct items and need to update items and not properties of those items, it seems that you have to use ObservableCollection. Another solution would be to derive from BindingList and implement INotifyCollectionChanged.

I am not an expert but this is what i have gathered during the last hour, if anyone has anything to add or correct please let me know.