简体   繁体   中英

Expand All WPF TreeView Nodes At Runtime Using WPF-MVVM

I want to expand all TreeView Items when user sets CheckBox I was able to do it at startup of the application using style :

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

And I was thinking if I can do some thing like this :

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

and in viewModel :

 public bool ExpandAllItems ;

but it didn't work. any body knows how to do such a thing ?

Just bind the TreeViewItem.IsExpanded to the CheckBox.IsChecked . Notice, that you must use converter to convert bool? to bool .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM