/// <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>
 /// Resets the <paramref name="sortDescriptors"/> collection to match the <paramref name="sortDescriptions"/> collection.
 /// </summary>
 /// <param name="sortDescriptions">The collection to match</param>
 /// <param name="sortDescriptors">The collection to reset</param>
 private static void ResetToSortDescriptions(SortDescriptionCollection sortDescriptions, SortDescriptorCollection sortDescriptors)
 {
     sortDescriptors.Clear();
     foreach (SortDescription description in sortDescriptions)
     {
         sortDescriptors.Add(SortCollectionManager.GetDescriptorFromDescription(description));
     }
 }
 private void AssertCollectionsAreEquivalent()
 {
     if (this._sourceCollection.Count != this._descriptionCollection.Count)
     {
         // Collections may not be equivalent if a descriptor isn't fully defined
         return;
     }
     Debug.Assert(SortCollectionManager.AreEquivalent(this._descriptionCollection, this._sourceCollection), "Collections should be equal.");
 }
 /// <summary>
 /// Resets the <paramref name="sortDescriptions"/> collection to match the <paramref name="sortDescriptors"/> collection.
 /// </summary>
 /// <param name="sortDescriptions">The collection to reset</param>
 /// <param name="sortDescriptors">The collection to match</param>
 private static void ResetToSortDescriptors(SortDescriptionCollection sortDescriptions, SortDescriptorCollection sortDescriptors)
 {
     sortDescriptions.Clear();
     foreach (SortDescriptor descriptor in sortDescriptors)
     {
         SortDescription?description = SortCollectionManager.GetDescriptionFromDescriptor(descriptor);
         if (description.HasValue)
         {
             sortDescriptions.Add(description.Value);
         }
     }
 }
 /// <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;
     }
 }
        /// <summary>
        /// Determines whether the <paramref name="sortDescriptions"/> are equivalent to the <paramref name="sortDescriptors"/>.
        /// </summary>
        /// <param name="sortDescriptions">The descriptions to compare</param>
        /// <param name="sortDescriptors">The descriptors to compare</param>
        /// <returns><c>true</c> if the collections are equivalent</returns>
        public static bool AreEquivalent(SortDescriptionCollection sortDescriptions, SortDescriptorCollection sortDescriptors)
        {
            Debug.Assert((sortDescriptions != null) && (sortDescriptors != null), "Both should be non-null.");

            if (sortDescriptions.Count != sortDescriptors.Count)
            {
                return(false);
            }

            for (int i = 0, count = sortDescriptions.Count; i < count; i++)
            {
                if (!SortCollectionManager.AreEquivalent(sortDescriptions[i], sortDescriptors[i]))
                {
                    return(false);
                }
            }

            return(true);
        }