ItemContainerGenerator 将在 Expanded 事件之后开始生成,因此您可以使用它来设置 Cursor,并在ItemContainerGenerator.StatusChanged触发时将其设置回来以指示您的孩子已被填充。
由于TreeViewItem.Expanded是一个路由事件,您可以在某个父级别订阅:
myTreeView.AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(TreeViewItemExpanded));
TreeViewItemExpanded 在其他地方定义,如下所示:
private void TreeViewItemExpanded(object sender, RoutedEventArgs e)
{
// we will only go through with this if our children haven't been populated
TreeViewItem sourceItem = e.OriginalSource as TreeViewItem;
if ((sourceItem != null)
&& (sourceItem.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated))
{
// create a handler that will check our children and reset the cursor when the ItemContainerGenerator has finished
EventHandler itemsGenerated = null;
DateTime before = DateTime.Now;
itemsGenerated = delegate(object o, EventArgs args)
{
// if the children are done being generated...
if ((o as ItemContainerGenerator).Status == GeneratorStatus.ContainersGenerated)
{
(o as ItemContainerGenerator).StatusChanged -= itemsGenerated; // we're done, so remove the handler
sourceItem.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, (ThreadStart)delegate // asynchronous reset of cursor
{
myWindow.Cursor = Cursors.Arrow; // reset cursor
Debug.WriteLine("Expanded in " + (DateTime.Now - before));
});
}
};
sourceItem.ItemContainerGenerator.StatusChanged += itemsGenerated; // add the handler
myWindow.Cursor = Cursors.Wait; // wait cursor
}
e.Handled = true;
}