WPF TreeView: Where is the ExpandAll() method

后端 未结 7 2082
抹茶落季
抹茶落季 2020-12-30 19:06

How can I expand all TreeView nodes in WPF? In WinForms there was a ExpandAll() method which does this.

7条回答
  •  攒了一身酷
    2020-12-30 19:41

    with XAML Treeview style you must have a property setter like that what wrote above :

    In Cs file, write methods like this, in my sample i used a button and my treeview's name is myTV :

    private void ExpandAll(ItemsControl items, bool expand)
        {
            foreach (object obj in items.Items)
            {
                ItemsControl childControl = items.ItemContainerGenerator.ContainerFromItem(obj) as ItemsControl;
                if (childControl != null)
                {
                    ExpandAll(childControl, expand);
                }
                TreeViewItem item = childControl as TreeViewItem;
                if (item != null)
                    item.IsExpanded = true;
            }
        }
    
    
        private void btnExpandAll_Click(object sender, RoutedEventArgs e)
        {
    
            foreach (object item in this.myTV.Items)
            {
                TreeViewItem treeItem = this.myTV.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                if (treeItem != null)
                    ExpandAll(treeItem, true);
                treeItem.IsExpanded = true;
            }
        }
    

    hope it could help you.

提交回复
热议问题