mirror of
https://github.com/hawkeye-stan/msfs-popout-panel-manager.git
synced 2024-11-24 23:00:12 +00:00
70 lines
2.5 KiB
C#
70 lines
2.5 KiB
C#
|
using PropertyChanged;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Collections.ObjectModel;
|
|||
|
using System.Collections.Specialized;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Linq;
|
|||
|
|
|||
|
namespace MSFSPopoutPanelManager.Shared
|
|||
|
{
|
|||
|
[SuppressPropertyChangedWarnings]
|
|||
|
public class SortedObservableCollection<T> : ObservableCollection<T> where T : IComparable<T>
|
|||
|
{
|
|||
|
public SortedObservableCollection() : base() { }
|
|||
|
|
|||
|
public SortedObservableCollection(IEnumerable<T> objects) : base(objects) { }
|
|||
|
|
|||
|
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
|
|||
|
{
|
|||
|
base.OnCollectionChanged(e);
|
|||
|
if (e.Action != NotifyCollectionChangedAction.Reset &&
|
|||
|
e.Action != NotifyCollectionChangedAction.Move &&
|
|||
|
e.Action != NotifyCollectionChangedAction.Remove)
|
|||
|
{
|
|||
|
var query = this.Select((item, index) => (Item: item, Index: index)).OrderBy(tuple => tuple.Item, Comparer.Default);
|
|||
|
var map = query.Select((tuple, index) => (OldIndex: tuple.Index, NewIndex: index)).Where(o => o.OldIndex != o.NewIndex);
|
|||
|
using (var enumerator = map.GetEnumerator())
|
|||
|
{
|
|||
|
if (enumerator.MoveNext())
|
|||
|
{
|
|||
|
base.MoveItem(enumerator.Current.OldIndex, enumerator.Current.NewIndex);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// (optional) user is not allowed to move items in a sorted collection
|
|||
|
protected override void MoveItem(int oldIndex, int newIndex) => throw new InvalidOperationException();
|
|||
|
protected override void SetItem(int index, T item) => throw new InvalidOperationException();
|
|||
|
|
|||
|
private class Comparer : IComparer<T>
|
|||
|
{
|
|||
|
public static readonly Comparer Default = new Comparer();
|
|||
|
|
|||
|
public int Compare(T x, T y) => x.CompareTo(y);
|
|||
|
}
|
|||
|
|
|||
|
public virtual void Sort()
|
|||
|
{
|
|||
|
if (Items.Count <= 1)
|
|||
|
return;
|
|||
|
|
|||
|
var items = Items.ToList();
|
|||
|
Items.Clear();
|
|||
|
items.Sort();
|
|||
|
foreach (var item in items)
|
|||
|
{
|
|||
|
Items.Add(item);
|
|||
|
}
|
|||
|
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
|
|||
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
|||
|
}
|
|||
|
|
|||
|
public ObservableCollection<string> Join(Func<object, object> value)
|
|||
|
{
|
|||
|
throw new NotImplementedException();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|