BeerMaker
BeerMaker

Reputation: 41

Expand all TreeView nodes

How can I expand all nodes in this code?

<TreeView ItemsSource="{Binding Path=.}" Height="220" HorizontalAlignment="Left" Margin="224,0,0,0" Name="treeView1" VerticalAlignment="Top" Width="162">
        <TreeView.ItemTemplate >
            <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding Path=IsChecked}" Focusable="False" Uid="{Binding Path=Id}" Click="CheckBox_Click"/>
                    <TextBlock Text="{Binding Path=Name}" Uid="{Binding Path=Id}"/>    
                </StackPanel>

            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

Upvotes: 0

Views: 2542

Answers (2)

Willem
Willem

Reputation: 9476

Use this:

<TreeView.ItemContainerStyle>
    <Style TargetType="TreeViewItem">
        <Setter Property="IsExpanded" Value="True" />
    </Style>
</TreeView.ItemContainerStyle>

Upvotes: 2

pchajer
pchajer

Reputation: 1584

You can have one boolean property e.g. IsExapnded on your class which gets binded with tree view item. Set the default value of IsExapnded to true and bind this with IsExpanded property of treeview item.

It will expand all the nodes.

OR if you want handle only in xaml, you can write like this :

<TreeView>
     <TreeView.ItemContainerStyle>         
       <Style TargetType="{x:Type TreeViewItem}">            
         <Setter Property="IsExpanded" Value="True" />         
       </Style>     
    </TreeView.ItemContainerStyle> 
</TreeView> 

Upvotes: 3

Related Questions