在我的WPF项目中,我需要一个可观察的集合,它始终保持正确的顺序。我的想法是使用一个SortedSet<T>
并实现自己的AddAndNotify
和RemoveAndNotify
方法。在这些方法中,我将调用NotifyPropertyChanged
,如下所示:
public class ObservableSortedSet<T> : SortedSet<T>, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
public void AddAndNotify(T element)
{
Add(element);
NotifyPropertyChanged(...);
}
public void RemoveAndNotify(T element)
{
Remove(element);
NotifyPropertyChanged(...);
}
}
但是哪个属性(或属性)会是那个呢?
如何实现一个集合,使UI在集合内容更改时更新?
还是说直接在我的ViewModel中使用SortedSet有更简单的方法?
编辑:
我不想使用预定义的ObservableCollection与排序视图一起使用。我知道可以使用CollectionViewSource或转换器来实现这一点,但这些解决方案并不吸引我。对于CollectionViewSource无法处理的分层数据,我认为转换器版本是对CollectionViewSource限制的可怕解决方法。我想使用一个干净的解决方案。
因此,这个问题不是如何对ObservableCollection进行排序的重复。我不想对ObservableCollection进行排序,我想使用一个SortedSet,它可以告诉UI变化。