Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observable Collection Property Changed on Item in the Collection

I have an ObservableCollection<T>. I've bound it to a ListBox control and I've added SortDescriptions to the Items collection on the ListBox to make the list sort how I want.

I want to resort the list at ANY point when any property changed on a child element.

All my child elements implement INotifyPropertyChanged.

like image 656
Nate Avatar asked Jun 18 '09 20:06

Nate


People also ask

How will an object be notified if the property bound to it has been changed?

Remarks. The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

What is difference between observable collection and list?

The true difference is rather straightforward:ObservableCollection implements INotifyCollectionChanged which provides notification when the collection is changed (you guessed ^^) It allows the binding engine to update the UI when the ObservableCollection is updated. However, BindingList implements IBindingList.

What is an observable collection?

An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated.

What is the use of observable collection?

ObservableCollection is a collection that allows code outside the collection be aware of when changes to the collection (add, move, remove) occur. It is used heavily in WPF and Silverlight but its use is not limited to there.


1 Answers

Brute force:

  1. Attach handler to each PropertyChanged event for each child item
  2. Grab the ListCollectionView from your CollectionViewSource
  3. Call Refresh.

EDIT:

The code for 1, 2 would live in your code-behind.

For #1, you'd do something like:

private void Source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    switch (e.Action)
    {
        case NotifyCollectionChangedAction.Add:
            foreach( SomeItem item in e.NewItems)
            {
               item.PropertyChanged += new PropertyChangedEventHandler(_SomeItem_PropertyChanged); 
            }
            break;
....
**HANDLE OTHER CASES HERE**
....
      }
}

For #2, in your CollectionChanged handler, you would do something like:

private void _SomeItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    ListCollectionView lcv = (ListCollectionView)(CollectionViewSource.GetDefaultView(theListBox.ItemsSource));
    lcv.Refresh();
}

EDIT2: However, in this case, I would strongly suggest that you also check ListCollectionView.NeedsRefresh and only refresh if that is set. There's no reason to re-sort if your properties have changed which don't affect the sort.

like image 71
micahtan Avatar answered Oct 04 '22 21:10

micahtan