How to expand all nodes in a TreeView
Hello,
I would like to expand all nodes in a TreeView defined as XAML by clicking onto the respective tool button. I'm not able to find a method like ExpandAll. Any idea?
Thank you,
Robert
Answers
Robert,
I think the easiest way to do this is to first define a style for a TreeView that contains a TreeViewItem style as a resource. (By putting the TreeViewItem style inside of the TreeView style you can control which TreeViews are expanded and which aren't). Once you've done that simply use the FindResource method to locate that Style in code and then set it to the TreeView that you want to expand:
Code Snippet<Window.Resources>
<Style x:Key="allExpanded" TargetType="TreeView"> <Style.Resources> <Style TargetType="TreeViewItem"> <Setter Property="IsExpanded" Value="True"/> </< FONT>Style> </< FONT>Style.Resources> </< FONT>Style> </< FONT>Window.Resources> <StackPanel> <Button Click="Button_Click">Toggle</< FONT>Button> <TreeView Style="{StaticResource allExpanded}" x:Name="tv"> <TreeViewItem Header="root1"> <TreeViewItem Header="Child1"> <TreeViewItem Header="Grandchild1"/> <TreeViewItem Header="Grandchild2"/> </< FONT>TreeViewItem> <TreeViewItem Header="Child2"/> </< FONT>TreeViewItem> <TreeViewItem Header="root2"> <TreeViewItem Header="Child1"> <TreeViewItem Header="Grandchild1"/> <TreeViewItem Header="Grandchild2"/> </< FONT>TreeViewItem> <TreeViewItem Header="Child2"/> </< FONT>TreeViewItem> </< FONT>TreeView> </< FONT>StackPanel>Code Snippetpublic partial class Window1 : Window
{
private Style _allExpanded;public Window1()
{
InitializeComponent();
_allExpanded = FindResource("allExpanded") as Style;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
tv.Style = tv.Style == _allExpanded ? null : _allExpanded;
}
}
Hope this helps,
Wells
Wells Caughey | Magenic TechnologiesThalion77 wrote: I would like to expand all nodes in a TreeView defined as XAML by clicking onto the respective tool button. I'm not able to find a method like ExpandAll. Any idea?
You can usually set the IsExpanded property to True and then call TreeView.UpdateLayout
All Replies
Robert,
I think the easiest way to do this is to first define a style for a TreeView that contains a TreeViewItem style as a resource. (By putting the TreeViewItem style inside of the TreeView style you can control which TreeViews are expanded and which aren't). Once you've done that simply use the FindResource method to locate that Style in code and then set it to the TreeView that you want to expand:
Code Snippet<Window.Resources>
<Style x:Key="allExpanded" TargetType="TreeView"> <Style.Resources> <Style TargetType="TreeViewItem"> <Setter Property="IsExpanded" Value="True"/> </< FONT>Style> </< FONT>Style.Resources> </< FONT>Style> </< FONT>Window.Resources> <StackPanel> <Button Click="Button_Click">Toggle</< FONT>Button> <TreeView Style="{StaticResource allExpanded}" x:Name="tv"> <TreeViewItem Header="root1"> <TreeViewItem Header="Child1"> <TreeViewItem Header="Grandchild1"/> <TreeViewItem Header="Grandchild2"/> </< FONT>TreeViewItem> <TreeViewItem Header="Child2"/> </< FONT>TreeViewItem> <TreeViewItem Header="root2"> <TreeViewItem Header="Child1"> <TreeViewItem Header="Grandchild1"/> <TreeViewItem Header="Grandchild2"/> </< FONT>TreeViewItem> <TreeViewItem Header="Child2"/> </< FONT>TreeViewItem> </< FONT>TreeView> </< FONT>StackPanel>Code Snippetpublic partial class Window1 : Window
{
private Style _allExpanded;public Window1()
{
InitializeComponent();
_allExpanded = FindResource("allExpanded") as Style;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
tv.Style = tv.Style == _allExpanded ? null : _allExpanded;
}
}
Hope this helps,
Wells
Wells Caughey | Magenic TechnologiesThalion77 wrote: I would like to expand all nodes in a TreeView defined as XAML by clicking onto the respective tool button. I'm not able to find a method like ExpandAll. Any idea?
You can usually set the IsExpanded property to True and then call TreeView.UpdateLayout
- Thank you for your help !!! I'm able to successfully expand all tree view nodes.


