Reputation: 21
I have a TreeView that I filled with a list of TreeViewItem from a collection (using the binding), I linked to a TreeView event "Expand" to expand it and display their childrens, my objective now is to get the last TreeViewItem accessed and to initialize the TreeView by the last treeViewItem consulted. I can retrieve the last TreeViewItem consulted and their childrens, but I can not expand it automatically. I use this method, but it returns always null.
TreeViewItem item =control.ItemContainerGenerator.ContainerFromItem(lastItem)as TreeViewItem; item.IsExpanded = true;
item is always null, isExpanded doses not work, and lastItem is a string(the text you see on the screen : is the treeViewItem.datacontext recovred ).
Upvotes: 2
Views: 3279
Reputation: 4002
Have you tried using the Expand Subtree method for TreeViewItem class?
The ExpandSubtree() method:
Expands the TreeViewItem control and all its child TreeViewItem elements.
Example:
TreeViewItem item = new TreeViewitem();
item.ExpandSubtree();
Upvotes: 0
Reputation: 8940
You should read this CodeProject article on TreeView MVVM approach which will simplify all your work with TreeView and TreeViewItem controls.
Upvotes: 2