C# 在树节点编辑器中禁用或灰显节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1462528/
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-08-06 17:48:07  来源:igfitidea点击:

Disable or grey out a node in the TreeNode Editor

c#visual-studiotreeviewvisibility

提问by mr-euro

How do I disable a specific node so the user can not select it. Hiding it for the user is also valid.

如何禁用特定节点以便用户无法选择它。为用户隐藏它也是有效的。

I tried the Visible property but that hides the entire tree (all nodes). I only want a few of the nodes disabled/hidden.

我尝试了 Visible 属性,但它隐藏了整个树(所有节点)。我只想要禁用/隐藏一些节点。

C# using Visual Studio 2005 TreeNode Editor.

C# 使用 Visual Studio 2005 树节点编辑器。

采纳答案by Fredrik M?rk

The TreeNodeitself does not have any Enabledproperty, so you will need to find some means of tracking that state. One way to do this is to create a new class that inherits TreeNodeand that features an Enabledproperty. Another way is to maintain a list of disabled tree nodes.

TreeNode本身没有任何Enabled财产,所以你需要找到跟踪该国的一些手段。一种方法是创建一个继承TreeNode并具有Enabled属性的新类。另一种方法是维护禁用树节点的列表。

Once that is done, you can use the ForeColorproperty of the TreeNodeto have it appear grayed out (for instance using the SystemColors.GrayTextvalue).

完成后,您可以使用 的ForeColor属性TreeNode使其显示为灰色(例如使用SystemColors.GrayText值)。

Finally you can use the BeforeSelectevent to evaluate whether it's OK for the user to select a particular node, and use the Cancelproperty of the event args in that event to prevent selecting it if that node is disabled:

最后,您可以使用BeforeSelect事件来评估用户是否可以选择特定节点,并Cancel在该事件中使用事件 args的属性来防止在该节点被禁用时选择它:

private void TreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
    e.Cancel = !NodeIsEnabled(e.Node);
}

回答by Daniel A. White

Two options:

两种选择:

  1. Add and remove the nodes on the fly.
  2. Owner draw and handle the clicks and send it to another node.
  1. 动态添加和删除节点。
  2. 所有者绘制并处理点击并将其发送到另一个节点。

回答by Itataki

I just found another way to handle the disabled treenodes. If you gray in the treenodes you don't want to use, you can ask for the color and not allow all grayed nodes.

我刚刚找到了另一种处理禁用树节点的方法。如果您在不想使用的树节点中变灰,则可以要求颜色并且不允许所有变灰的节点。

    private void TreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
    {
        if(SystemColors.GrayText==e.Node.ForeColor)
            e.Cancel = true;
    }

回答by WantToDo

set disabled node by yourNode.SelectAction = TreeNodeSelectAction.None

设置禁用节点 yourNode.SelectAction = TreeNodeSelectAction.None

I think you need also disable expandable this node yourNode.PopulateOnDemand = false

我认为您还需要禁用可扩展此节点 yourNode.PopulateOnDemand = false