我有一个 TreeView
,它使用一个带有自定义 IComprarer
的 ListCollectionView
和实时整形来对其子项进行排序。当当前选择的 TreeViewItem
在 View 中重新排序时,我希望 TreeView
自动滚动到 TreeViewItem
的新位置.但是,我找不到在 ListCollectionView
应用新排序时收到通知的方法,而且我想要的行为似乎没有内置到 TreeViewControl
中。
有什么方法可以在 ListCollectionView
重新计算其排序顺序时通知我?
最佳答案
我相信事件 CollectionChanged
是您所需要的。你可以这样做:
((ICollectionView)yourListCollectionView).CollectionChanged += handler;
之所以要在这里强制转换是因为CollectionChanged
是作为INotifyPropertyChanged
的成员实现的(ICollectionView
继承自该接口(interface)),源码这里:
event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged
{
add {
CollectionChanged += value;
}
remove {
CollectionChanged -= value;
}
}
这个实现是明确的。因此,作为公共(public)成员,该事件对正常访问是隐藏的。要公开该成员,您可以将该实例转换为 ICollectionView
或 INotifyPropertyChanged
。
.显式实现接口(interface)时,必须先将实例显式转换为该接口(interface),然后才能访问接口(interface)成员。
接口(interface)实现示例:
public interface IA {
void Test();
}
//implicitly implement
public class A : IA {
public void Test() { ... }
}
var a = new A();
a.Test();//you can do this
//explicitly implement
public class A : IA {
void IA.Test() { ... } //note that there is no public and the interface name
// is required
}
var a = new A();
((IA)a).Test(); //this is how you do it.
关于c# - CollectionView 实时排序回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26066645/