/// <summary>
 /// Synchronizes the group descriptions collection to the group descriptors collection.
 /// </summary>
 /// <param name="descriptor">The descriptor that changed</param>
 /// <param name="e">The property change event</param>
 private void HandleGroupDescriptorChanged(GroupDescriptor descriptor, PropertyChangedEventArgs e)
 {
     this._ignoreChanges = true;
     try
     {
         // We have to reset when the collections were not equal before the change
         if (this._sourceCollection.Count != this._descriptionCollection.Count)
         {
             GroupCollectionManager.ResetToGroupDescriptors(this._descriptionCollection, this._sourceCollection);
         }
         else
         {
             int index = this._sourceCollection.IndexOf(descriptor);
             GroupDescription description = GroupCollectionManager.GetDescriptionFromDescriptor(descriptor);
             if (description == null)
             {
                 this._descriptionCollection.RemoveAt(index);
             }
             else
             {
                 this._descriptionCollection[index] = description;
             }
         }
     }
     finally
     {
         this._ignoreChanges = false;
     }
 }
 /// <summary>
 /// Resets the <paramref name="groupDescriptions"/> collection to match the <paramref name="groupDescriptors"/> collection.
 /// </summary>
 /// <param name="groupDescriptions">The collection to reset</param>
 /// <param name="groupDescriptors">The collection to match</param>
 private static void ResetToGroupDescriptors(ObservableCollection <GroupDescription> groupDescriptions, GroupDescriptorCollection groupDescriptors)
 {
     groupDescriptions.Clear();
     foreach (GroupDescriptor descriptor in groupDescriptors)
     {
         GroupDescription description = GroupCollectionManager.GetDescriptionFromDescriptor(descriptor);
         if (description != null)
         {
             groupDescriptions.Add(description);
         }
     }
 }
 /// <summary>
 /// Synchronizes the group descriptions collection to the group descriptors collection.
 /// </summary>
 /// <param name="e">The collection change event</param>
 private void HandleGroupDescriptorCollectionChanged(NotifyCollectionChangedEventArgs e)
 {
     this._ignoreChanges = true;
     try
     {
         // We have to reset in a number of situations
         // 1) Resetting the GroupDescriptors
         // 2) Collections were not equal before replacing GroupDescriptors
         // 3) Collections were not equal before removing GroupDescriptors
         // 4) Collections were not equal before adding GroupDescriptors
         if ((e.Action == NotifyCollectionChangedAction.Reset) ||
             ((e.Action == NotifyCollectionChangedAction.Replace) && ((this._sourceCollection.Count + e.OldItems.Count) != (this._descriptionCollection.Count + e.NewItems.Count))) ||
             ((e.Action == NotifyCollectionChangedAction.Remove) && ((this._sourceCollection.Count + e.OldItems.Count) != this._descriptionCollection.Count)) ||
             ((e.Action == NotifyCollectionChangedAction.Add) && (this._sourceCollection.Count != (this._descriptionCollection.Count + e.NewItems.Count))))
         {
             GroupCollectionManager.ResetToGroupDescriptors(this._descriptionCollection, this._sourceCollection);
         }
         else
         {
             if ((e.Action == NotifyCollectionChangedAction.Remove) ||
                 (e.Action == NotifyCollectionChangedAction.Replace))
             {
                 int index = e.OldStartingIndex;
                 if (e.Action == NotifyCollectionChangedAction.Replace) // TODO: This is a DependencyObjectCollection bug!
                 {
                     index = e.NewStartingIndex;
                 }
                 for (int i = 0; i < e.OldItems.Count; i++)
                 {
                     this._descriptionCollection.RemoveAt(index);
                 }
             }
             if ((e.Action == NotifyCollectionChangedAction.Add) ||
                 (e.Action == NotifyCollectionChangedAction.Replace))
             {
                 int index = e.NewStartingIndex;
                 foreach (object item in e.NewItems)
                 {
                     GroupDescription description = GroupCollectionManager.GetDescriptionFromDescriptor((GroupDescriptor)item);
                     if (description != null)
                     {
                         this._descriptionCollection.Insert(index++, description);
                     }
                 }
             }
         }
     }
     finally
     {
         this._ignoreChanges = false;
     }
 }