当前位置:  开发笔记 > 编程语言 > 正文

如何为WPF Treeview挂钩BeforeNodeExpand和AfterNodeExpand?

如何解决《如何为WPFTreeview挂钩BeforeNodeExpand和AfterNodeExpand?》经验,为你挑选了1个好方法。



1> Robert Macne..:

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

推荐阅读
和谐啄木鸟
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有