开发者

Is there a way to make a TreeView appear always fully expanded?

I have a TreeView on my开发者_C百科 form but I'd like it to always be fully expanded, even the child nodes.

Is there a way?


Do you want it to initially display expanded? If so, then call the ExpandAll method on the root node after you have added all of the tree nodes.

If you want it to display expanded and not allow the user to collapse it, then you need to handle the BeforeCollapsed event and cancel it. (Set e.Cancel = true;).


One way is to use TreeView.ExpandAll() like this:

private void myCheckBox_CheckedChanged(object sender, System.EventArgs e)
{
   // If the check box is checked, expand all the tree nodes.
   if (myCheckBox.Checked == true)
   {
      myTreeView.ExpandAll();
   }
   else
   {
      myTreeView.CollapseAll();
   }
}


In WPF, one way is to keep your tree view fully expanded:

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


Guessing here... On the BeforeCollapsed event, set e.Cancel = true;


TreeView.ExpandAll Method


Try the TreeNode.EnsureVisible Method - [1]: https://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.ensurevisible(v=vs.90).aspx


Use this code:

private void ManagerFolder_Load(object sender, EventArgs e)
{
    treeView.ExpandAll();
    treeView.Nodes[0].FirstNode.Collapse();
    treeView.Nodes[4].FirstNode.Collapse();
}


You may be wondering where to put the treeview1.expandAll() statement? You cannot put it in the Designer's generated code, or it will be deleted at the next form refresh/save.

I've put it in the "Form1.cs" code, in the partial class part which deals with the form initialization, and it works. Just after the InitializeComponent() statement (if you don't know what I'm talking about, just find that statement)

Hope it helps :)

PD. Thanks for your support, you're great, guys!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜