Skip to content

Instantly share code, notes, and snippets.

@Krumelur
Created December 12, 2014 07:10
Show Gist options
  • Save Krumelur/527db13ac36fffecb5b5 to your computer and use it in GitHub Desktop.
Save Krumelur/527db13ac36fffecb5b5 to your computer and use it in GitHub Desktop.
GroupedObservableCollection
/// <summary>
/// Grouped observable collection.
/// KEY_TYPE = the type used for the group key
/// ITEM_TYPE = the type used for the items of the list.
/// </summary>
public class GroupedObservableCollection<KEY_TYPE, ITEM_TYPE> : ObservableCollection<ITEM_TYPE>
{
/// <summary>
/// Gets the grouping key.
/// </summary>
public KEY_TYPE Key
{
get;
private set;
}
/// <summary>
/// Initializes a new instance of the <see cref="MyContacts.GroupedObservableCollection`2"/> class.
/// </summary>
/// <param name="key">group key (for a list of names this could be a string representing the first character of the name)</param>
/// <param name="groupItems">group items. These will be copied into the grouped collection</param>
/// <param name="masterCollectionToWatch">
/// Master collection to watch. This is optional. If provided, the master collection will be watched for changes and the
/// grouped collection will update itself according to these changes.
/// Note: GroupedObservableCollection will subscribe to the CollectionChanged event of the master collection.
/// </param>
public GroupedObservableCollection(KEY_TYPE key, IEnumerable<ITEM_TYPE> groupItems, INotifyCollectionChanged masterCollectionToWatch = null)
{
this.Key = key;
// Copy all the group items over.
foreach (var item in groupItems)
{
this.Items.Add (item);
}
// Watch the master if it is set. Whenever something changes, we also update ourselves.
if (masterCollectionToWatch != null)
{
masterCollectionToWatch.CollectionChanged += HandleMasterCollectionChanged;
}
}
void HandleMasterCollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
{
switch(e.Action)
{
case NotifyCollectionChangedAction.Remove:
// Remove old items from our group.
foreach (ITEM_TYPE item in e.OldItems)
{
Debug.WriteLine ("GroupedObservableCollection removing [{0}]", item);
this.Remove(item);
}
break;
case NotifyCollectionChangedAction.Add:
// Add items to our group.
foreach (ITEM_TYPE item in e.NewItems)
{
Debug.WriteLine ("GroupedObservableCollection adding [{0}]", item);
this.Add(item);
}
break;
case NotifyCollectionChangedAction.Replace:
// TODO: Implement. Need to check what is in the event props.
// Replace items.
foreach (ITEM_TYPE item in e.OldItems)
{
Debug.WriteLine ("GroupedObservableCollection replacing old item [{0}]", item);
}
// Replace items.
foreach (ITEM_TYPE item in e.NewItems)
{
Debug.WriteLine ("GroupedObservableCollection replacing new item [{0}]", item);
}
Debug.WriteLine ("-----");
break;
case NotifyCollectionChangedAction.Move:
// TODO: Implement. Need to check what is in the event props.
// Move items.
foreach (ITEM_TYPE item in e.OldItems)
{
Debug.WriteLine ("GroupedObservableCollection moving old item [{0}]", item);
}
// Move items.
foreach (ITEM_TYPE item in e.NewItems)
{
Debug.WriteLine ("GroupedObservableCollection moving new item [{0}]", item);
}
Debug.WriteLine ("-----");
break;
case NotifyCollectionChangedAction.Reset:
Debug.WriteLine ("GroupedObservableCollection clearing content");
this.Clear ();
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment