开发者

Get FullPath in WPF treeview?

开发者 https://www.devze.com 2023-01-27 07:17 出处:网络
If I create a WPF TreeView programmatically, for example: // TreeView treeView; <- added in the designer

If I create a WPF TreeView programmatically, for example:

// TreeView treeView; <- added in the designer
TreeViewItem rootNode = 开发者_Go百科new TreeViewItem();
rootNode.Header = "RootNode"
treeView.Items.Add(rootNode);

TreeViewItem subNode1 = new TreeViewItem();
subNode1.Header = "SubNode1";
rootNode.Items.Add(subNode1);

TreeViewItem subNode2 = new TreeViewItem();
subNode2.Header = "SubNode2";
rootNode.Items.Add(subNode2);

However, I would like to get the path to a certain node in the treeview much like the FullPath property in the System.Windows.Forms.TreeNode class. So the path for subNode2 would be RootNode//SubNode2. How do I do this?

Edit: I am not looking for the manual solution, however I am wondering if there is an equivalent accessor like FullPath or if I'm going about using the WPF TreeView class in the wrong way. Eg. I've looked at DisplayMemberPath and SelectedValuePath but they don't seem to provide the result I'm after.


Take a look in my answer here:

Silverlight: Determine parent TreeViewItem?

Determine path then is simple:

public string GetFullPath(TreeViewItem node) 
{
  if (node == null)
    throw new ArgumentNullException();

  var result = Convert.ToString(node.Header);

  for (var i = GetParentItem(node); i != null; i = GetParentItem(i))
    result = i.Header + "\\" + result;

  return result;
}


See below code.

So this function returns a stack in which top most item will be root and last item will be the immediate parent of element.

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        TreeViewItem rootNode = new TreeViewItem();
        rootNode.Header = "RootNode";
        treeView.Items.Add(rootNode); 

        TreeViewItem subNode1 = new TreeViewItem(); 
        subNode1.Header = "SubNode1"; 
        rootNode.Items.Add(subNode1); 

        TreeViewItem subNode2 = new TreeViewItem(); 
        subNode2.Header = "SubNode2"; 
        rootNode.Items.Add(subNode2);

        TreeViewItem subNode3 = new TreeViewItem();
        subNode3.Header = "SubNode3";
        subNode2.Items.Add(subNode3);  
    }

    public static Stack<TreeViewItem> GetNodeParent(UIElement element)
    {
        Stack<TreeViewItem> tempNodePath = new Stack<TreeViewItem>();
        // Walk up the element tree to the nearest tree view item. 
        TreeViewItem container = element as TreeViewItem;

        while ((element != null))
        {
            element = VisualTreeHelper.GetParent(element) as UIElement;
            container = element as TreeViewItem;
            if(container!=null)
            tempNodePath.Push(container);
        }

        return tempNodePath;
    }

    private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        Stack<TreeViewItem> path = GetNodeParent(e.NewValue as UIElement);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消