Reputation: 1162
I have a WPF application with a number of ComboBoxes. Each ComboBox is bound to a SortedList, lets call this the datasource. The reason I chose this type for the datasource is simply because I'll be getting updates to the TValues as well and new items and potentially deletes although they are less likely. I wanted a quicker and easier way of finding the items when a data event (add, update, delete happens). I was then binding to a property of type IList and returning _mySortedList.Values in the getter.
However I quickly realised that this wasn't going to work because I don't get notifications of changes pumped through to the UI like you would with an ObservableCollection.
So I'm wondering what is the best approach to having all my items sorted (the sort condition never changes but could be based on multiple properties of the object) and having the nice automatic notifications of ObservableCollection.
Many thanks in advance for any help you can give.
Upvotes: 0
Views: 517
Reputation: 5785
The easiest way to do this is to create a child class that implements INotifyCollectionChanged and also has Sort functionality. If you are already using a SortedList, then you can simply create a class that derives from SortedList, implements INotifyCollectionChanged and overrides the Add/Remove/etc. methods to raise the NotifyCollectedChanged event.
It may look something like this (incomplete):
public class SortedObservableList : SortedList, INotifyCollectionChanged
{
public override void Add(object key, object value)
{
base.Add(key, value);
RaiseCollectionChanged(NotifyCollectionChangedAction.Add);
}
public override void Remove(object key)
{
base.Remove(key);
RaiseCollectionChanged(NotifyCollectionChangedAction.Remove);
}
#region INotifyCollectionChanged Members
protected void RaiseCollectionChanged(NotifyCollectionChangedAction action)
{
if (CollectionChanged != null)
CollectionChanged(this, new NotifyCollectionChangedEventArgs(action));
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
#endregion
}
Alternatively, you could create a class that derives from ObservableCollection and implements sorting functionality, but that may not make sense if you're already using a SortedList.
EDIT: Your comment below and further review of the question reveals that you are using the generic version of the SortedList (SortedList). In that case, you can have your SortableObservableList implement the IDictionary interface (and/or ICollection, IEnumerable), and use a SortedList internally to store the items. Here is a snippet of the code you can use (did not include all implemented methods since they are just pass-throughs to your internal sorted list.)
public class SortedObservableList<TKey, TValue> : IDictionary<TKey, TValue>, INotifyCollectionChanged
{
private SortedList<TKey, TValue> _list;
public SortedObservableList()
{
_list = new SortedList<TKey, TValue>();
}
#region INotifyCollectionChanged Members
protected void RaiseCollectionChanged(NotifyCollectionChangedAction action)
{
if (CollectionChanged != null)
CollectionChanged(this, new NotifyCollectionChangedEventArgs(action));
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
#endregion
#region IDictionary<TKey,TValue> Members
public void Add(TKey key, TValue value)
{
_list.Add(key, value);
this.RaiseCollectionChanged(NotifyCollectionChangedAction.Add);
}
public bool ContainsKey(TKey key)
{
return _list.ContainsKey(key);
}
public ICollection<TKey> Keys
{
get { return _list.Keys; }
}
public bool Remove(TKey key)
{
bool result = _list.Remove(key);
this.RaiseCollectionChanged(NotifyCollectionChangedAction.Remove);
return result;
}
//etc...
#endregion
}
Upvotes: 2