Example #1
0
 void SortKeyChanged(object sender, SortKeyChangedEventArgs e)
 {
     if (!WeakThis.TryGetTarget(out var @this))
     {
         this.FileReference.SortKeyChanged -= this.SortKeyChanged;
     }
     else
     {
         @this.RecomputeComponents();
     }
 }
        private void Item_SortKeyChanged(object sender, SortKeyChangedEventArgs e)
        {
            if (_handlingSortKeyChanged)
            {
                return;
            }

            _handlingSortKeyChanged = true;

            try
            {
                if (sender is FileReference item)
                {
                    var surrogate = new FileReference()
                    {
                        SortKey = e.NewSortKey
                    };

                    item.SortKey = e.OldSortKey;

                    int oldIndex = _list.BinarySearch(item);
                    int newIndex = _list.BinarySearch(surrogate);

                    if (oldIndex < 0)
                    {
                        _list.Remove(item);
                    }
                    else
                    {
                        _list.RemoveAt(oldIndex);
                    }

                    if (newIndex < 0)
                    {
                        newIndex = ~newIndex;
                    }

                    if (oldIndex < newIndex)
                    {
                        newIndex--;
                    }

                    item.SortKey = e.NewSortKey;

                    _list.Insert(newIndex, item);
                }
            }
            finally
            {
                _handlingSortKeyChanged = false;
            }
        }