使用WPF-MVVM在运行时展开所有WPF TreeView节点

时间:2014-12-12 08:17:39

标签: c# wpf xaml mvvm treeview

我希望在用户设置CheckBox时展开所有TreeView项目我可以在使用样式启动应用程序时执行此操作:

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

我在想是否能做这样的事情:

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

并在viewModel中:

 public bool ExpandAllItems ;

但它不起作用。任何人都知道如何做这样的事情?

1 个答案:

答案 0 :(得分:1)

只需将TreeViewItem.IsExpanded绑定到CheckBox.IsChecked即可。请注意,您必须使用转换器将bool?转换为bool