WPF树视图:ExpandAll()方法在哪里

2vuwiymt  于 2023-02-10  发布在  其他
关注(0)|答案(9)|浏览(156)

如何在WPF中展开所有的TreeView节点?在WinForms中有一个ExpandAll()方法可以完成此操作。

np8igboo

np8igboo1#

这可能会有帮助

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

xxslljrj2#

对于XAML树视图样式,您必须有一个类似上面所写的属性设置器:
在Cs文件中,编写如下方法,在我的示例中,我使用了一个按钮,我的树形视图的名称是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;
        }
    }

希望对你有帮助。

fiei3ece

fiei3ece3#

WPF TreeView类没有ExpandAll方法。因此,必须循环遍历节点并将其IsExpanded属性设置为true。

trnvg8h3

trnvg8h34#

试试这个

private void btnExpandAll_Click(object sender, RoutedEventArgs e)
{
    foreach(var item in tvES.Items)
    {
        var tvi = item as TreeViewItem;
        if (tvi != null)
            tvi.ExpandSubtree();
    }
}
mspsb9vt

mspsb9vt5#

@Pierre-Olivier给出的答案很好。
就我个人而言,在你不知道数据嵌套有多深的情况下,我更喜欢使用堆栈而不是递归。更好的是,它可以作为一个扩展函数提供:

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);
}
z8dt9xmd

z8dt9xmd6#

现在是2022-01-14,我用的是VS 2022和.net6,我相信在2015年~现在,MS构建了一个名为ExpandSubTree()的新方法,它可以展开的不是整个树,而是单个TreeViewItem,这让事情变得更简单。
CollapseAll有很大的不同,因为只需要折叠第一层。

public static class TreeViewHelper
{
    public static void ExpandAll(this TreeView treeView)
    {
        foreach (var item in treeView.Items)
        {
            if (treeView.ItemContainerGenerator.ContainerFromItem(item) is TreeViewItem treeViewItem)
                treeViewItem.ExpandSubtree();
        }
    }

    public static void CollapseAll(this TreeView treeView)
    {
        foreach (var item in treeView.Items)
        {
            if (treeView.ItemContainerGenerator.ContainerFromItem(item) is TreeViewItem treeViewItem)
                treeViewItem.IsExpanded = false;
        }
    }

    public static void CollapseAll(this TreeViewItem treeViewItem)
    {
        foreach (var item in treeViewItem.Items)
        {
            if (treeViewItem.ItemContainerGenerator.ContainerFromItem(item) is TreeViewItem subTreeViewItem)
                subTreeViewItem.IsExpanded = false;
        }
    }
}
axzmvihb

axzmvihb7#

除了user 2779123的评论之外,我知道这个问题很久以前就得到了回答,但是我还建议Pierre-OlivierPignon的代码推送treeItem.IsExpanded = true;不仅移动到空检查的范围内,而且通过将其移动到ExpandAll过程,因为它看起来是以允许树结构的展开和折叠的格式编写的,并且将其移动到那里将通过设计向该功能添加根节点。
如下例所示:

private void ExpandAll(ItemsControl items, bool expand)
{
    items.IsExpanded = 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);
    }
}
zwghvu4y

zwghvu4y8#

/// <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();
            }
        }
    }
}
p4rjhz4m

p4rjhz4m9#

虽然其他的解决方案也能起作用,但有一种简单得多的方法可以做到这一点,它几乎是各种React的混合。

public void ExpandAll(TreeView treeView)
{
    foreach (object item in treeView.Items)
        if (trvStory.ItemContainerGenerator.ContainerFromItem(item) is TreeViewItem treeItem)
            treeItem.ExpandSubtree();
}

现在,您只需要创建一个调用它的方法:

private void btnExpandAll_Click(object sender, RoutedEventArgs e)
{
    ExpandAll(trvItems);
}

(其中trvItems是树视图的名称)。

相关问题