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

Advertisements