WPF TreeView:ExpandAll()方法在哪里

时间:2009-05-07 11:14:42

标签: wpf treeview

如何在WPF中展开所有TreeView节点?在WinForms中有一个ExpandAll()方法来执行此操作。

7 个答案:

答案 0 :(得分:73)

这可能会有所帮助

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

答案 1 :(得分:17)

使用XAML Treeview样式,您必须具有类似上面所写的属性设置器:                   

在Cs文件中,写这样的方法,在我的示例中我使用了一个按钮,我的treeview的名字是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;
        }
    }
希望它可以帮助你。

答案 2 :(得分:12)

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

答案 3 :(得分:4)

试试这个

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

答案 4 :(得分:2)

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

答案 5 :(得分:0)

除了user2779123的评论,我知道这已经很久了,但我也建议Pierre-Olivier Pignon的代码推送treeItem.IsExpanded = true;不仅要移动到null检查的范围,而是将其移动到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);
    }
}

答案 6 :(得分:0)

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