Ete*_*l21 3 sorting wpf datagrid observablecollection icollectionview
我有以下绑定到 DataGrid 的 ObservableCollection:
public ObservableCollection<Message> Messages = new ObservableCollection<Message>;
Run Code Online (Sandbox Code Playgroud)
XAML:
<DataGrid ItemsSource="{Binding Path=Messages}">
Run Code Online (Sandbox Code Playgroud)
我在启动时对其进行排序,使用默认视图:
ICollectionView view = CollectionViewSource.GetDefaultView(Messages);
view.SortDescriptions.Add(new SortDescription("TimeSent", ListSortDirection.Descending));
Run Code Online (Sandbox Code Playgroud)
一切正常,但问题是每当我向 Messages 集合添加新消息时,它只会附加到列表的底部,而不是自动排序。
Messages.Add(message);
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?我确信我可以通过每次添加项目时刷新视图来解决这个问题,但这似乎是错误的做法(更不用说性能方面了)。
所以我做了更多的调查,结果我的问题是由于 WPF 数据网格的限制。当基础数据发生变化时,它不会自动重新排序集合。换句话说,当您第一次添加项目时,它会被排序并放置在正确的位置,但如果您更改项目的属性,则不会重新排序。INotifyPropertyChanged 与排序更新无关。它只处理更新显示的数据,但不触发对其进行排序。强制重新排序的是 CollectionChanged 事件,但修改集合中已有的项目不会触发此特定事件,因此不会执行任何排序。
这是另一个类似的问题: C# WPF Datagrid 不会对数据更新进行动态排序
该用户的解决方案是手动调用 OnCollectionChanged()。
最后,我结合了这两个线程的答案:
我还添加了“智能”排序,如果更改的属性是当前在 SortDescription 中使用的值,则仅调用 OnCollectionChanged()。
public class MessageCollection : ObservableCollection<Message>
{
ICollectionView _view;
public MessageCollection()
{
_view = CollectionViewSource.GetDefaultView(this);
}
public void Sort(string propertyName, ListSortDirection sortDirection)
{
_view.SortDescriptions.Clear();
_view.SortDescriptions.Add(new SortDescription(propertyName, sortDirection));
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
this.AddPropertyChanged(e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
this.RemovePropertyChanged(e.OldItems);
break;
case NotifyCollectionChangedAction.Replace:
case NotifyCollectionChangedAction.Reset:
this.RemovePropertyChanged(e.OldItems);
this.AddPropertyChanged(e.NewItems);
break;
}
base.OnCollectionChanged(e);
}
private void AddPropertyChanged(IEnumerable items)
{
if (items != null)
{
foreach (var obj in items.OfType<INotifyPropertyChanged>())
{
obj.PropertyChanged += OnItemPropertyChanged;
}
}
}
private void RemovePropertyChanged(IEnumerable items)
{
if (items != null)
{
foreach (var obj in items.OfType<INotifyPropertyChanged>())
{
obj.PropertyChanged -= OnItemPropertyChanged;
}
}
}
private void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
bool sortedPropertyChanged = false;
foreach (SortDescription sortDescription in _view.SortDescriptions)
{
if (sortDescription.PropertyName == e.PropertyName)
sortedPropertyChanged = true;
}
if (sortedPropertyChanged)
{
NotifyCollectionChangedEventArgs arg = new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Replace, sender, sender, this.Items.IndexOf((Message)sender));
OnCollectionChanged(arg);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3583 次 |
| 最近记录: |