zoukankan      html  css  js  c++  java
  • WPF TreeView 选择事件执行两次,获取TreeView的父节点的解决方法

    1.TreeView选择事件执行两次

    Very often, we need to execute some code in SelectedItemChanged depending on the selected TreeViewItem. ButSelectedItemChanged is called twice. This is due to stealing focus from the main window, which is screwing something up.

    What we have to do to avoid this is simply delay the call to our code, i.e., MyFunction() which we need to execute inSelectedItemChanged. Here's a workaround which delays the call to open the new window until the item selection code finishes up:

    private delegate void NoArgDelegate();
     
    void Window1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
      Dispatcher.BeginInvoke(DispatcherPriority.Background, 
            (NoArgDelegate)delegate { MyFunction(); });
    }
    

    2.Treeview获取父节点

       private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
            {
                //节点(是子节点或者是根节点)
                TreeViewItem item = treeView1.SelectedItem as TreeViewItem;
     
                //获取父节点
                TreeViewItem parent = item.Parent as TreeViewItem;
                //判断父节点是否存在
                if (parent != null) {
                    //显示父节点信息,这里显示 Header 信息
                    MessageBox.Show("父节点的Header:" + parent.Header.ToString());
                } else {
                    MessageBox.Show("没有父节点!");
                }
     
            }
    

      

      

  • 相关阅读:
    nginx反向代理、让代理节点记录客户端真实IP
    Nginx反向代理多虚拟主机代理
    Nginx负载均衡
    Python装饰器笔记
    Python多线程学习笔记
    Python+Bottle+Sina SAE快速构建网站
    Python中使用ElementTree解析xml
    [转]浅谈Python web框架
    Python数据库连接池实例——PooledDB
    了解HTML的代码注释
  • 原文地址:https://www.cnblogs.com/smiler/p/3927628.html
Copyright © 2011-2022 走看看