#1,219 – Expanding All Nodes in a TreeView by Default

Normally, when you display a TreeView, the nodes are collapsed by default and the user clicks on the expander widgets to expand nodes that they want to look at.

You can expand all nodes in the TreeView by default by setting up an ItemContainerStyle for the TreeView and specifying that you want each TreeViewItem expanded.

Below, we show the first part of a TreeView definition in XAML, referencing an ItemContainerStyle (data templates are not shown).

<TreeView Grid.Row="0" Margin="5" 
          ItemsSource="{Binding Breeds}" 
          ItemContainerStyle="{StaticResource TreeViewItemStyle_ExpandAll}" 
          HorizontalContentAlignment="Stretch">
<!-- remainder of TreeView definition goes here -->

In the style, we simply set the IsExpanded property to true.

<Style x:Key="TreeViewItemStyle_ExpandAll" TargetType="{x:Type TreeViewItem}">
    <Setter Property="IsExpanded" Value="True"/>
</Style>

Now, at runtime, all of the nodes in the TreeView are expanded by default.

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

4 Responses to #1,219 – Expanding All Nodes in a TreeView by Default

  1. Pingback: Dew Drop - September 29, 2017 (#2572) - Morning Dew

  2. farth says:

    Thanks for your tutorial. I make own style of treeview. I want mark every parent when child is selected. Is there any easy way to do that? Thanks

  3. SezMe says:

    After droping in to 2000 Things for the umpteeenth time, I just wanted to note that I think this is one of the clearest, straightforward and useful .net site I’ve visited. Many kudos. Keep on truckin’

Leave a comment