0

I'm using a TreeView and the HierarchicalDataTemplate to show some data. Sometimes the quantity of this data is quite much, so I would like to implement a button, which collapses the TreeView completely and another button, which expands it.

My XAML of the TreeView looks like this for now:

<TreeView x:Name="constOutput" Margin="25,96,25,229" SelectedItemChanged="ConstOutput_SelectedItemChanged" ItemsSource="{Binding FoundConstants}">

      <TreeView.Resources>
        <Style TargetType="TreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}">
          <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
        <HierarchicalDataTemplate DataType="{x:Type local:Constant}" ItemsSource="{Binding Children}">
          <WrapPanel HorizontalAlignment="Stretch">
            <TextBlock Text="{Binding Path=Name}"/>
            <TextBlock Text="{Binding Path=SrcFile.Name, StringFormat='({0})'}" Foreground="#006666" FontSize="13" HorizontalAlignment="Right"  Margin="10, 2, 0, 0"/>
            <TextBlock Text="{Binding Path=Children.Count, StringFormat='[{0}]'}" FontSize="13" HorizontalAlignment="Right" Margin="10, 2, 0, 0"/>
          </WrapPanel>
          <HierarchicalDataTemplate.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:Constant}" ItemsSource="{Binding Children}">
              <WrapPanel>
                <TextBlock Text="{Binding Path=Name}"/>
                <TextBlock Text="{Binding Path=SrcFile.Name, StringFormat='({0})'}" Foreground="#006666" FontSize="13" Margin="10, 2, 0, 0"/>
                <TextBlock Text="{Binding Path=Children.Count, StringFormat='[{0}]'}" FontSize="13" HorizontalAlignment="Right" Margin="10, 2, 0, 0"/>
              </WrapPanel>
              <HierarchicalDataTemplate.ItemTemplate>

Im also using a ControlTemplate for the TreeView.

I think, I need to loop through all the elements of the TreeView and set all of the nodes on IsExpanded=false. I just don't know, where I would set it.

Szwajcer
  • 11
  • 1
  • 3
  • Try the answers on [this page](https://stackoverflow.com/questions/834081/wpf-treeview-where-is-the-expandall-method/15346816#15346816). – Mark Feldman May 18 '22 at 22:04
  • It worked so far, I need to figure out how to make it done with MVVM in mind. Thanks. – Szwajcer May 23 '22 at 06:12

0 Answers0