C#TreeView折叠/展开父选择 [英] C# TreeView Collapse/Expand on parent select

查看:246
本文介绍了C#TreeView折叠/展开父选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个很难解释,所以我只告诉你我想要实现的目标以及我的工作方式,所以也许有人会对我做错的事情有所了解。

This one is hard to explain so i will just tell you what i want to achieve and how i am doing it so maybe someone witll have an idea on what i am doing wrong.

在我的winForm中,我有一个填充的TreeView控件。所有父母都有孩子(不限数目),但没有孩子有自己的孩子。例如:

In my winForm i have a TreeView control that i populate. All parents have children (any number of them) but no children have children of their own. Example:

- Parent 1
        Child 1
        Child 2
        Child 3
- Parent 2
       Child 4

我想要的行为如下:


  1. 当用户单击一个孩子时(默认情况下完成)

  2. 当用户单击一个父项时,它应该折叠或展开但不更改任何选择

点1没问题,因为它是TreeView的默认行为,第二点是另一个故事。

Point 1 is no problem as it is the default behaviour of a TreeView, Point 2 however is another story.

这是我到目前为止所做的:

Here is what i have done so far:

为了防止父母从被选中开始,我添加一个BeforeSelect事件处理程序,并在其中添加以下代码:

To prevent the parent from being selected i add a BeforeSelect event handler and put in it the following code:

if (e.Node.Parent == null)
    e.Cancel = true;

这可以完美地完成工作。因此,现在我取消了对父项的选择,我想对其进行扩展或折叠。所以我将上面的代码更改为:

This does the job perfectly. So now that i canceled the selection of the parent i want to expand or collapse it. So i changed the above code to:

if (e.Node.Parent == null)
{
    if (e.Node.IsExpanded)
    {
        e.Node.Collapse();
    }
    else
    {
        e.Node.Expand();
    }
    e.Cancel = true;
}

这有点奏效-区别在于,与单击+号不同,调用Expand( )或Collapse()使父节点被选中,而忽略e.Cancel = true;

This somewhat works - except that, unlike clicking the + sign, calling Expand() or Collapse() makes the parent node be selected, ignoring the e.Cancel = true; line.

我尝试在调用Collapse()或Expand()之前获取SelectedNode,然后将其重新设置-它可以工作,但是当我这样做时,它将扩展父对象再次进行选择。

I tried getting the SelectedNode before i call Collapse() or Expand() and then setting it back - it works but when i do that it will expand the parent again to make the selection.

当我点击父母时,+号确实可以实现我想要的功能-我似乎无法使其正常工作。

Somewhow the + sign does exactly what i want to happen when i click on a parent - i just can't seem to get it to work.

想法?

预先感谢

推荐答案

尝试以下操作:

    private bool allowExpandCollapse = false;

    private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
    {
        e.Cancel = !allowExpandCollapse;
    }

    private void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
    {
        e.Cancel = !allowExpandCollapse;
    }

    private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
    {
        if (e.Node.Parent == null)
        {
            e.Cancel = true;
        }
    }

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Node.Parent == null)
        {
            allowExpandCollapse = true;
            if (e.Node.IsExpanded)
            {
                TreeNode selectedNode = treeView1.SelectedNode;
                e.Node.Collapse();
                allowExpandCollapse = false;
                treeView1.SelectedNode = selectedNode;
            }
            else
            {
                e.Node.Expand();
                allowExpandCollapse = false;
            }
        }
    }

这篇关于C#TreeView折叠/展开父选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆