1

我有一个TreeView使用 aListCollectionView的自定义IComprarer和实时塑造来订购它的孩子。当当前选择TreeViewItem的视图在视图中重新排序时,我希望TreeView自动滚动到TreeViewItem的新位置。但是,我找不到在ListCollectionView应用新排序时收到通知的方法,而且我想要的行为似乎没有内置到TreeViewControl.

有没有办法在ListCollectionView重新计算排序顺序时通知我?

4

1 回答 1

6

我相信这个活动CollectionChanged是你所需要的。你可以这样做:

((ICollectionView)yourListCollectionView).CollectionChanged += handler;

我们这里要强制转换的原因CollectionChanged是实现为INotifyPropertyChangedICollectionView继承自该接口)的成员,源代码在这里:

event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged
{
    add {
            CollectionChanged += value;
    }
    remove {
            CollectionChanged -= value;
    }
}

这个实现是明确的。因此,该事件对于作为公共成员的正常访问是隐藏的。要公开该成员,您可以将实例强制转换为ICollectionViewINotifyPropertyChanged

. 显式实现接口时,必须先将实例显式转换为该接口,然后才能访问接口成员。

关于实现接口的示例:

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.
于 2014-09-26T20:10:18.213 回答