/// <summary>
 /// Synchronizes the sort descriptions collection to the sort descriptors collection.
 /// </summary>
 /// <param name="descriptor">The descriptor that changed</param>
 /// <param name="e">The property change event</param>
 private void HandleSortDescriptorChanged(SortDescriptor 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)
         {
             SortCollectionManager.ResetToSortDescriptors(this._descriptionCollection, this._sourceCollection);
         }
         else
         {
             int             index       = this._sourceCollection.IndexOf(descriptor);
             SortDescription?description = SortCollectionManager.GetDescriptionFromDescriptor(descriptor);
             if (!description.HasValue)
             {
                 this._descriptionCollection.RemoveAt(index);
             }
             else
             {
                 this._descriptionCollection[index] = description.Value;
             }
         }
     }
     finally
     {
         this._ignoreChanges = false;
     }
 }
 /// <summary>
 /// Synchronizes the sort descriptions collection to the sort descriptors collection.
 /// </summary>
 /// <param name="e">The collection change event</param>
 private void HandleSortDescriptorCollectionChanged(NotifyCollectionChangedEventArgs e)
 {
     this._ignoreChanges = true;
     try
     {
         // We have to reset in a number of situations
         // 1) Resetting the SortDescriptors
         // 2) Collections were not equal before replacing SortDescriptors
         // 3) Collections were not equal before removing SortDescriptors
         // 4) Collections were not equal before adding SortDescriptors
         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))))
         {
             SortCollectionManager.ResetToSortDescriptors(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)
                 {
                     SortDescription?description = SortCollectionManager.GetDescriptionFromDescriptor((SortDescriptor)item);
                     if (description.HasValue)
                     {
                         this._descriptionCollection.Insert(index++, description.Value);
                     }
                 }
             }
         }
     }
     finally
     {
         this._ignoreChanges = false;
     }
 }