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; }