WPF TreeView: Where is the ExpandAll() method

后端 未结 7 2048
抹茶落季
抹茶落季 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:25

    This might help

    <TreeView>
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="True" />
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView>
    
    0 讨论(0)
  • 2020-12-30 19:26

    The WPF TreeView class does not have an ExpandAll method. Thus you'd have to loop through the nodes and set their IsExpanded properties to true.

    0 讨论(0)
  • 2020-12-30 19:26
        /// <summary>
        /// Collapse the TreeView.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _collapseTreeView_button_Click(object sender, RoutedEventArgs e)
        {
            _ordersParentChild_TreeView.UpdateLayout();
            if (_ordersParentChild_TreeView.Items.Count > 0)
            {
                var rootTreeViewItem = _ordersParentChild_TreeView.Items[0] as TreeViewItem;
                if (rootTreeViewItem != null)
                {
                    rootTreeViewItem.IsExpanded = false;
                }
            }
        }
        /// <summary>
        /// Expand the TreeView.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _expandTreeView_button_Click(object sender, RoutedEventArgs e)
        {
            _ordersParentChild_TreeView.UpdateLayout();
            if(_ordersParentChild_TreeView.Items.Count > 0)
            {
                var rootTreeViewItem = _ordersParentChild_TreeView.Items[0] as TreeViewItem;
                if (rootTreeViewItem != null)
                {
                    rootTreeViewItem.ExpandSubtree();
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 19:33

    try this

    private void btnExpandAll_Click(object sender, RoutedEventArgs e)
    {
        foreach(var item in tvES.Items)
        {
            var tvi = item as TreeViewItem;
            if (tvi != null)
                tvi.ExpandSubtree();
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-30 19:41

    The answer provided by @Pierre-Olivier is a good one.

    Personally, I prefer to use a stack rather than recursion in circumstances such as these where you have no idea how deeply nested the data could be. Even better, it could be provided as an extension function:

    public static void ExpandAll(this TreeViewItem treeViewItem, bool isExpanded = true)
    {
        var stack = new Stack<TreeViewItem>(treeViewItem.Items.Cast<TreeViewItem>());
        while(stack.Count > 0)
        {
            TreeViewItem item = stack.Pop();
    
            foreach(var child in item.Items)
            {
                var childContainer = child as TreeViewItem;
                if(childContainer == null)
                {
                    childContainer = item.ItemContainerGenerator.ContainerFromItem(child) as TreeViewItem;
                }
    
                stack.Push(childContainer);
            }
    
            item.IsExpanded = isExpanded;
        }
    }
    
    public static void CollapseAll(this TreeViewItem treeViewItem)
    {
        treeViewItem.ExpandAll(false);
    }
    
    0 讨论(0)
提交回复
热议问题