WPF 中的自动展开树视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1487144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:02:58  来源:igfitidea点击:

AutoExpand treeview in WPF

wpftreeviewexpand

提问by David Brunelle

Is there a way to automatically expand all nodes from a treeview in WPF? I searched and didn't even find an expand function in the treeview property.

有没有办法从 WPF 中的树视图中自动展开所有节点?我搜索了,甚至没有在 treeview 属性中找到扩展功能。

Thanks

谢谢

回答by Anvaka

You can set ItemContainerStyle and use IsExpanded property.

您可以设置 ItemContainerStyle 并使用 IsExpanded 属性。

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Grid>
      <TreeView>
         <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
               <Setter Property="IsExpanded" Value="True"/>
            </Style>
         </TreeView.ItemContainerStyle>
         <TreeViewItem Header="Header 1">
            <TreeViewItem Header="Sub Item 1"/>
         </TreeViewItem>
         <TreeViewItem Header="Header 2">
            <TreeViewItem Header="Sub Item 2"/>
         </TreeViewItem>
      </TreeView>
   </Grid>
</Page>

If you need to do this from code, you can write viewmodel for your tree view items, and bind IsExpanded property to corresponding one from model. For more examples refer to great article from Josh Smith on CodeProject: Simplifying the WPF TreeView by Using the ViewModel Pattern

如果您需要从代码中执行此操作,您可以为树视图项编写视图模型,并将 IsExpanded 属性绑定到模型中的相应属性。有关更多示例,请参阅 Josh Smith 在 CodeProject 上的精彩文章:Simplifying the WPF TreeView by Using the ViewModel Pattern

回答by Carlo

This is what I use:

这是我使用的:

private void ExpandAllNodes(TreeViewItem rootItem)
{
    foreach (object item in rootItem.Items)
    {
        TreeViewItem treeItem = (TreeViewItem)item;

        if (treeItem != null)
        {
            ExpandAllNodes(treeItem);
            treeItem.IsExpanded = true;
        }
    }
}

In order for it to work you must call this method in a foreach loop for the root node:

为了使其工作,您必须在根节点的 foreach 循环中调用此方法:

// this loop expands all nodes
foreach (object item in myTreeView.Items)
{
    TreeViewItem treeItem = (TreeViewItem)item;

    if (treeItem != null)
    {
        ExpandAllNodes(treeItem);
        treeItem.IsExpanded = true;
    }
}

回答by jwize

Carlo's answer was better because it opens all levels

卡罗的答案更好,因为它打开了所有级别

This improves upon that example with a little more concise code example.

这通过更简洁的代码示例改进了该示例。

    private void ExpandAllNodes(TreeViewItem treeItem)
    {
        treeItem.IsExpanded = true;  
        foreach (var childItem in treeItem.Items.OfType<TreeViewItem>())
        {
                ExpandAllNodes(childItem);
        }
    }

Call it by using this line of code

使用这行代码调用它

TreeViewInstance.Items.OfType<TreeViewItem>().ToList().ForEach(ExpandAllNodes);

回答by Darío Andrés Mu?oz Prudant

if you want expand manually you can try

如果您想手动扩展,您可以尝试

Xaml:

Xml:

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

c#:

C#:

bool Expanded = false; 
// The event subscription method (for a button click)
private void ButtonExpand__Click(object sender, RoutedEventArgs e)
{
    Expanded = !Expanded;
    Style Style = new Style
    {
        TargetType = typeof(TreeViewItem)
    };

    Style.Setters.Add(new Setter(TreeViewItem.IsExpandedProperty, Expanded));
    TreePeople.ItemContainerStyle = Style;
}

回答by gmmarcilli

Another programmatical way to manipulate full expansion of tree items, maybe via c# code, is using the TreeViewItem.ExpandSubTree()command on a root node.

另一种操作树项完全扩展的编程方式(可能通过 c# 代码)是TreeViewItem.ExpandSubTree()在根节点上使用该命令。

private void ExpandFirstRootNode()
{
   TreeViewControl.Items[0].ExpandSubtree();
}