Get notified of collection changes - c#

My class has a public List field. I want to detect when someone changes this collection. However, writing my own Add/Remove wrapper around this collection seems wasteful. Is there a way to get notifications about changes in collection with delegates or something like that?

Use ObservableCollection<T> instead
you may find reference here.

There already exists a collection you've described and it's called ObservableCollection.
It has CollectionChanged event, so just subscribe your event handler there and i'll get called every time item is added or removed from collection..

Use BindingList<T> instead. It offers a ListChanged event. It is also accepted as DataSource in many controls such as a Listbox or a Grid.
As mentioned in this answer the BindingList offers a lot more than ObservableCollection
If you want to change a list in a listbox and changes to that list be reflected to the UI, you can set a BindingList<> as datasource to the Listbox and then simply manipulate the list. The Listbox will handle the ListChanged event and will display changes without having to manually do it by your self.
If you are going to use it in Winforms you should go with BindingList and if you are going to use it on a WPF app then go with ObservableCollection.

Related

How do I detect when an item is added to a ListBox?

I've looked all over and can find nothing. The only solution I've found is to manually call the function when you add an item, but that's inefficient, and I know it's possible.
Wrap the logic in a separate class. That way you're only calling the one piece of code but you can also know when items are added.
There is no event for this that is native to the .NET listbox control. The only other option is subclassing the listbox and listening manually.
Would it not be easier in your case to Bind the ListBox.Items collection to an ObservableCollection so you can subscribe to the CollectionChanged event?

is this the way BindingList should work?

I have a DevExpress GridControl bound to a BindingList. I used a BindingList so that changes are automatically applied to the bound control.
It works perfectly when I add or remove items from the list, but when I change an item, it doesn't quit work the way I want it.
I created a foreach loop that runs in another thread (to keep the UI thread free to update) that iterates thru all the objects in the BindingList and changes some of their properties.
The way I expect it to work is that each property updated updates on the GridControl in real time. However the updating is very "chunky". Sometimes it updates 2 rows at a time, sometimes I have to click the GridControl to get the new values.
Why does this happen?
Is it a good solution to call DataControl.RefreshDataSource() after each item?
but when I change an item, it doesn't quit work the way I want it.
Becasue changes within an item are not somethign the BindingList cares about -that is why the items should implement INotifyPropertyChanged.
Basically the binding list says when the LIST has changed, but if you update a property - how should the list know ;) And why should it care - every item is separately responsible for publishing updates to its properties using said interface.
Is it a good solution to call DataControl.RefreshDataSource() after each item?
Worst way. Make sure the items implement INotifyPropertyChange and raise the PropertyChanged event accordingly.
The objects in your BindingList should implement the INotifyPropertyChanged interface and raise the PropertyChanged event when the value of their properties change.

ComboBox doesn't automatically update when DataSource changes?

For some reason when adding or remove items from the DataSource (a simple BindingList) the ComboBox updates accordingly but if I edit an item like this, it doesn't update automatically:
myBindingList[index].Name = "NewName";
myBindingList[index].Value = newValue;
In order to get it to update when I edit an item as opposed to creating or removing an item I have to do this after the change is made:
myComboBox.DataSource = null;
myComboBox.DataSource = myBindingList;
This fixes the problem but it seems like a rather messy solution. Also with large lists it may become slow (premature optimization I know) but still is there a way to force the ComboBox to update without completely re-assigning its DataSource?
Thanks for reading.
this is stated in the MSDN forums:
The IBindingList interface contains the ListChanged event where
controls like the combobox hook up into if the underlying datasource
assigned to it implements the said interface. your datasource must
raise the corresponding ListChanged with proper ListChangeEventArgs if
ever you add, remove, change, etc. your IBindingList implementor.
this way, whenever the underlying source you used to bind to your
combobox is changed, the corresponding UI control (combobox) is
refreshed.
you say you are using BindingList and in fact you do get the combobox to reflect add or remove items events. I think you should do the update of the items already inside your BindingList in another way because looks like the proper BindingList events are not firing.
you could either investigate into that or simply live with reset and reassign the DataSource, I don't think is too bad, you are in Statefull Windows Forms application not in SatetLess Webforms so you do have your objects there all the time :)
You need observable collections and IPropertyChange implementation:
ComboBox bound to a ObservableCollection does not update

INotifyCollectionChanged -- How often does it fire (and how do they make it so efficient/fast)?

Basically, I'm wondering how it is actually efficient here.
Sample code:
void GetItems()
{
foreach (var item in items)
myObservableCollection.Add(item);
}
Won't this fire off the CollectionChanged event every time causing the UI to have to refresh everytime? Or does it do it so that it waits til the GetItems function is done?
Basically, it seems that WPF handles it very well, and I'm wondering how they did that.
Optimizing Performance: Data Binding provides some background on how data bindings are resolved, including the performance implications of different items sources. Take a look at the Binding to an ItemsSource section.
Consider a scenario in which you have a CLR List object that holds
a list of employees that you want to display in a ListBox. To create a
correspondence between these two objects, you would bind your employee
list to the ItemsSource property of the ListBox. However, suppose you
have a new employee joining your group. You might think that in order
to insert this new person into your bound ListBox values, you would
simply add this person to your employee list and expect this change to
be recognized by the data binding engine automatically.
That assumption would prove false; in actuality, the change will not
be reflected in the ListBox automatically. This is because the CLR
List object does not automatically raise a collection changed
event. In order to get the ListBox to pick up the changes, you would
have to recreate your list of employees and re-attach it to the
ItemsSource property of the ListBox. While this solution works, it
introduces a huge performance impact. Each time you reassign the
ItemsSource of ListBox to a new object, the ListBox first throws away
its previous items and regenerates its entire list. The performance
impact is magnified if your ListBox maps to a complex DataTemplate.
A very efficient solution to this problem is to make your employee
list an ObservableCollection. An ObservableCollection object
raises a change notification which the data binding engine can
receive. The event adds or removes an item from an ItemsControl
without the need to regenerate the entire list.
Update time for 1 item (ms)
To a CLR List object = 1656 ms
To an ObservableCollection = 20 ms
WPF never binds directly to a collection. If you specify a collection as a binding source, WPF actually binds to the collection's default view.
A collection view is a layer on top of a binding source collection
that allows you to navigate and display the source collection based on
sort, filter, and group queries, without having to change the
underlying source collection itself. A collection view also maintains
a pointer to the current item in the collection. If the source
collection implements the INotifyCollectionChanged interface, the
changes raised by the CollectionChanged event are propagated to the
views.
The event will fire for every change.
The GUI does not have to react and refresh every time, it can postpone that.
I know WinForms will optimize this, I think WPF has a similar approach.
If you want to see how often the UI requests the fresh results expose it as a public property and put a debug line in the get (assessor) of the public property for myObservableCollection.

BindingList equivalent in Silverlight? Want to observe changes in collection elements

I was looking around for something like ObservableCollection<T> that would also raise an event whenever the properties of item in the collection change. I came across BindingList<T>, but was disappointed to learn that it's not available in Silverlight.
I'm aware that I could build this fairly easily by subclassing ObservableCollection<T> and subscribing to the PropertyChanged events of items as they are added (and unsubscribing as they are removed, of course). Before I do that, though, I want to make sure there isn't something I can use out of the box.
The WPF way to do this is to have the objects that you're putting in the ObservableCollection inherit from DependencyObject and have DependencyProperties instead of old fashioned properties and PropertyChanged events. This may mean you have to create a wrapper object that implements this stuff. This will make bindings from UI to the objects in your collection automatically update.
If, on the other hand, you're not doing databinding, and instead you just want a way to get an event to fire in code whenever any property on one of your objects changes, you'd probably need to create your own collection to do this.
http://msdn.microsoft.com/en-us/library/ms752914.aspx has a good explanation of DependencyProperties and examples of the implementation.

Categories