private static void OnComparerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CollectionSortBehavior behavior = (CollectionSortBehavior)d;

            SortComparer oldComparer = (SortComparer)e.OldValue;

            behavior.UnregisterComparer(oldComparer);

            SortComparer newComparer = (SortComparer)e.NewValue;

            behavior.RegisterComparer(newComparer);

            behavior.SortItems();
        }
        private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CollectionSortBehavior behavior = (CollectionSortBehavior)d;

            IEnumerable oldSource = (IEnumerable)e.OldValue;

            behavior.UnregisterItemsSource(oldSource);

            IEnumerable newSource = (IEnumerable)e.NewValue;

            behavior.RegisterItemsSource(newSource);

            SortComparer comparer = behavior.Comparer;

            if (comparer != null)
            {
                comparer.ItemsSource = newSource;
            }

            behavior.SortItems();
        }