2

I currently have a simple tree view that contains one parent node with multiple child nodes. I was wondering if there is a way to find the location of the selected node in the parent tree.

I currently have an action event on the treeview and when the user clicks on the child it prints out the string value of the selected child. I have tried using:

int val = TreeView.SelectedItemProperty.GlobalIndex;

but it always returns 0. I have seen some examples in VB but I cant seem to get the same idea to work in C#.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Johnston
  • 2,873
  • 8
  • 29
  • 39
  • Are you looking for cartesian coordinates? or a nested path like "grandparent\parent\child"? – agent-j Jun 15 '11 at 01:25
  • Im loooking for the order in the parent node it is. if my treeview looks like this: >parent -child0 -child1 -child2 -child3 i want to get the value "2" if the user clicks on child 2 (assuming the counter starts at 0) – Johnston Jun 15 '11 at 01:28
  • Thinking genericly about a treeview, why does there need to be an order? That is often determined of the presentation panel you choose (ContainerTemplate). Is there some other way you can identify your children? – agent-j Jun 15 '11 at 01:34
  • i have a vector of the items and i use that to create the children. I want the spot the child is so that i can link it back to the vector since the vector element would be the same value as the order number the child is in the treeview – Johnston Jun 15 '11 at 01:41
  • Correct me if I'm wrong, but you have the /entry/ of the vector. Why not do a vector.IndexOf(tv.SelectedItem)? – agent-j Jun 15 '11 at 01:46
  • im not sure if this changes anything but its a list. It doesnt seem to like myList.IndexOf(mytreeview.selectedItem) im assuming tv is my treeView. my intellisense says that it wants listType item (which is the treeview), int index, intcount – Johnston Jun 15 '11 at 02:01
  • What is mytreeview.selectedItem.GetType()? Isn't that the type of data in your vector (and your treeview)? – agent-j Jun 15 '11 at 02:03
  • that just retuns the value of the child. Also going back to the list idea. My list uses a struct as a type. (i believe its consider 3d) inside of each element is another 3 values – Johnston Jun 15 '11 at 02:08

3 Answers3

1

You have to use the ItemContainerGenerator property of the Treeview.

http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcontainergenerator.aspx

See: ContainerFromIndex and IndexFromContainer

Note that each TreeViewItem also has an ItemContainerGenerator (its an ItemsControl), so you'd have to recursively search down the tree if you have multiple levels.

Adam
  • 1,202
  • 11
  • 25
1

I think the answer to all your treeview problems (and most ui ones) in wpf is to build a ViewModel. Anytime you start crawling the visual tree to look for elements that you are already binding to, you are doing things the hard way. Once you start using ItemsContainerGenerator you have to start worrying about a whole lot of issues you should not have to.

You are binding to a hierarchical structure. If that structure has a selected item property on each item and it is bound to the TreeViewItem selected item then you can just get the selected item in code and do everything else from there. Have a look at a similiar question here.

Community
  • 1
  • 1
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
0

So i didn't find the answer i was looking for (I may of confused others with what my question was. by saying location). Anyways how I solved it was I got the string value of the child selected and compared it to my list. Thanks to those who answered!

Johnston
  • 2,873
  • 8
  • 29
  • 39