Nat*_*ate 11 c# sorting collections wpf observablecollection
我有一个ObservableCollection<T>.我已将它绑定到ListBox控件,并且我已添加SortDescriptions到ListBox上的Items集合中,以使列表按我的方式排序.
当任何属性在子元素上发生更改时,我想在任何时候使用列表.
我所有的子元素都实现了INotifyPropertyChanged.
mic*_*tan 12
蛮力:
编辑:
1,2的代码将存在于您的代码隐藏中.
对于#1,您可以执行以下操作:
private void Source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach( SomeItem item in e.NewItems)
{
item.PropertyChanged += new PropertyChangedEventHandler(_SomeItem_PropertyChanged);
}
break;
....
**HANDLE OTHER CASES HERE**
....
}
}
Run Code Online (Sandbox Code Playgroud)
对于#2,在CollectionChanged处理程序中,您可以执行以下操作:
private void _SomeItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
ListCollectionView lcv = (ListCollectionView)(CollectionViewSource.GetDefaultView(theListBox.ItemsSource));
lcv.Refresh();
}
Run Code Online (Sandbox Code Playgroud)
EDIT2:但是,在这种情况下,我强烈建议您也检查ListCollectionView.NeedsRefresh并仅在设置时刷新.如果您的属性已更改而不影响排序,则没有理由重新排序.
| 归档时间: |
|
| 查看次数: |
20369 次 |
| 最近记录: |