我有一个MVVM应用程序,并且数据将网格绑定到可观察的集合。该集合的排序如下:
public ObservableCollection<MyObject> MyData
{
get
{
return _myData;
}
set
{
_myData = new ObservableCollection<MyObject>(value.OrderByDescending(s => s.DateParam));
RaisePropertyChanged(() => MyData);
}
}
我添加条目时工作正常,但日期MyObject.DateParam
可能会发生变化。尽管对象和集合都如图所示引发NotifyPropertyChanged
,但它不会更新。我的解决方法如下:
SelectedDataEntry.DateParam = DateTime.Now;
MyData = MyData;
请有人告诉我,这样做的方式更为流畅。
答案 0 :(得分:1)
您提出的问题与提升ObservableCollection
的已更改事件无关,而只是关于如何提供有序的可观察集合,该集合响应ObservableCollections项目的INotifyPropertyChanged
。
我建议使用自定义集合,该集合实现INotifyCollectionChanged
。 ObservableCollection
不支持您要求的功能,也不支持WPF的CollectionViewSource
,这是WPF内部排序和过滤集合的解决方案。
这是我的实现,我还没有测试过,但是你应该按照预期的方式工作:
/// <summary>
/// ObservableCollection, supporting sorting of items
/// which automatically refreshes when items notify property changes.
/// </summary>
/// <typeparam name="T">Item type</typeparam>
public sealed class SortableObservableCollection<T> : INotifyCollectionChanged, ICollection<T>
{
private readonly IComparer<T> _comparer;
private readonly List<T> _innerItems;
public SortableObservableCollection(IComparer<T> comparer)
{
_comparer = comparer;
_innerItems = new List<T>();
}
public SortableObservableCollection(IEnumerable<T> collection, IComparer<T> comparer)
{
_comparer = comparer;
_innerItems = new List<T>(collection);
}
private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Sort();
}
public void Sort()
{
_innerItems.Sort(_comparer);
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
private void RaiseAdd(T newItem)
{
if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newItem));
}
private void RaiseRemoved(T oldItem)
{
if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItem));
}
private void RaiseReset()
{
if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public IEnumerator<T> GetEnumerator()
{
return _innerItems.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(T item)
{
// Subscribe to items change notification
var inpc = item as INotifyPropertyChanged;
if (inpc != null) inpc.PropertyChanged += Item_PropertyChanged;
_innerItems.Add(item);
Sort();
RaiseAdd(item);
RaiseReset();
}
public void Clear()
{
foreach (var innerItem in _innerItems)
{
var inpc = innerItem as INotifyPropertyChanged;
if (inpc != null) inpc.PropertyChanged -= Item_PropertyChanged;
}
_innerItems.Clear();
RaiseReset();
}
public bool Contains(T item)
{
return _innerItems.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
_innerItems.CopyTo(array, arrayIndex);
}
public bool Remove(T item)
{
// Unsubscribe from item's change notification
var inpc = item as INotifyPropertyChanged;
if (inpc != null) inpc.PropertyChanged -= Item_PropertyChanged;
var removed = _innerItems.Remove(item);
if (!removed) return false;
Sort();
RaiseRemoved(item);
RaiseReset();
return true;
}
public int Count { get { return _innerItems.Count; }}
public bool IsReadOnly { get { return false; } }
}
它基本上包含List<T>
并在相应操作(ObservableCollection<T>
,Add(T)
,Remove(T)
)上引发与Clear()
相同的事件。此外,它会在添加或删除项目时执行Sort()
操作,并在项目更改时订阅每个项目的INotifyPropertyChanged
事件以执行排序。注意,集合Reset
各种各样的提出。这不是一个高性能的解决方案,但您可以通过实现自定义排序操作根据您的需要对其进行优化。
答案 1 :(得分:0)
为什么不创建像:
这样的可观察集合private ObservableCollection<MyObject> _myData = new ObservableCollection<MyObject>();
public ObservableCollection<MyObject> MyData
{
get{ return _myData;}
}
然后例如
MyData[0].DateParam = DateTime.Now;
MyData.OrderByDescending(s => s.DateParam);