WPF 中的自动展开树视图

发布于 2024-08-06 21:56:34 字数 74 浏览 5 评论 0原文

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

谢谢

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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷心人i 2024-08-13 21:56:34

您可以设置 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>

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

You can set ItemContainerStyle and use IsExpanded property.

<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

陪你搞怪i 2024-08-13 21:56:34

这就是我使用的:

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

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

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

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

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

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:

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

    if (treeItem != null)
    {
        ExpandAllNodes(treeItem);
        treeItem.IsExpanded = true;
    }
}
两个我 2024-08-13 21:56:34

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

Xaml:

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

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

if you want expand manually you can try

Xaml:

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

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;
}
悲念泪 2024-08-13 21:56:34

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

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

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

使用这行代码调用它

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

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);
三生一梦 2024-08-13 21:56:34

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

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

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

private void ExpandFirstRootNode()
{
   TreeViewControl.Items[0].ExpandSubtree();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文