2

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 ).

2
  • You should specify it's the System.Windows.Controls.TreeView (WPF) version of TreeView. Jul 20, 2010 at 15:21
  • firstly thank you for your answer, But where I should specify the version
    – user396932
    Jul 20, 2010 at 15:48

2 Answers 2

2

You should read this CodeProject article on TreeView MVVM approach which will simplify all your work with TreeView and TreeViewItem controls.

0
0

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.