AutoExpand treeview in WPF

泄露秘密 提交于 2021-02-06 09:35:31

问题


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


回答1:


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




回答2:


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



回答3:


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



回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/1487144/autoexpand-treeview-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!