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("没有父节点!");
      }
 
    }

 

 /// <summary>
        /// 获取父节点的完整路径
        /// </summary>
        /// <param name="TV"></param>
        /// <returns></returns>
        string GetItemFather(TreeViewItem TV)
        {
            //获取父节点
            TreeViewItem parent = TV.Parent as TreeViewItem;
            //判断父节点是否存在
            if (parent != null)
            {
                return  GetItemFather(parent)+"\\"+parent .Header .ToString ();
            }
            else
            {
                return "";
            }
        }

 

posted @ 2015-05-21 13:05  nieweiking  阅读(4002)  评论(0编辑  收藏  举报