繁体   English   中英

如何禁用树节点复选框?

[英]How can I disable a treenode checkbox?

我正在使用来自 codeproject 的 treeviewExtensions,它实现了一些 iEnumerable 递归,以便在检查节点时检查所有父节点和子节点。

接下来我想为我实现一种禁用特定节点的方法。 我已通过 GUI 将颜色设置为灰色来指示节点不可用。 但是,节点旁边的复选框仍然是启用的,并且没有禁用/启用属性。 有什么办法可以解决这个问题吗?

未禁用 Node10 的示例

如果 TreeNode 被禁用,您没有明确指定您想要的内容。 您是否只想要一些特殊的颜色,或者如果 TreeNode 被禁用,您是否想要带有 Checked state 和子节点的东西?

所以你想要一个具有一些特殊行为的 TreeNode 吗?

在您OO class 中,您了解到,如果您想要与其他事物具有几乎相同行为的事物,则应考虑从另一个事物派生。

class TreeNodeThatCanBeDisabled : TreeNode // TODO: invent proper name
{
    // Coloring when enabled / disabled
    public Color EnabledForeColor {get; set;} = base.ForeColor;
    public Color EnabledBackColor {get; set;} = base.BackColor;
    public Color DisabledForeColor {get; set;} = ...
    public Color DisabledBackColor {get; set;} = ...

    private bool isEnabled = true;

    public bool IsEnabled
    {
        get => this.isEnable;
        set
        {
            it (this.IsEnabled = value)
            {
                // TODO: set the colors
                this.isEnabled = value;
            }
        }
    }
}

也许您想在 IsEnabled 更改时引发一个事件,我不确定每个节点执行此操作是否明智:

public event EventHandler IsEnabledChanged;
protected virtual void OnEnabledChanged(EventHandle e)
{
    this.IsEnabledChanged?.Invoke(this, e);
}

在 IsEnabled Set 中调用它。

此外:你想用复选标记做什么? 并且所有子节点也应该被禁用吗?

foreach (TreeNodeThatCanBeDisabled subNode in this.Nodes.OfType<TreeNodeThatCanBeDisabled())
{
    subNode.IsEnabled = value;
}

而且我认为您应该创建一个可以一次启用/禁用多个 TreeNode 的 TreeNodeView,并且可以为您提供所有启用/禁用的节点。

TODO:决定这个特殊的 TreeNodeView 是否可能只包含 TreeNodesThatCanBeDisabled,或者也包含标准 TreeNodes。

class TreeNodeViewThatCanHoldTreeNodesThatCanBeDisabled : TreeNodeView // TODO: proper name
{
    // Coloring when enabled / disabled
    public Color EnabledForeColor {get; set;} = base.ForeColor;
    public Color EnabledBackColor {get; set;} = base.BackColor;
    public Color DisabledForeColor {get; set;} = ...
    public Color DisabledBackColor {get; set;} = ...

    public void AddNode(TreeNodeThatCanBeDisabled treeNode)
    {
        this.Nodes.Add(treeNode);
    }

    public IEnumerable<TreeNodeThatCanBeDisabled> TreeNodesThatCanBeDisabled =>
        base.Nodes.OfType<TreeNodeThatCanBeDisabled>();

    public IEnumerable<TreeNodeThatCanBeDisabled> DisabledNodes =>
        this.TreeNodesThatCanBeDisabled.Where(node => !node.IsEnabled);

    public void DisableAll()
    {
        foreach (var treeNode in this.TreeNodesThatCanBeDisabled)
            treeNode.Enabled = false;
    }

TODO:您是否只想更改 colors? 还是复选框? 折叠/展开? 也许一个事件会告诉你:“嘿,伙计,这个 treeNode 已被禁用”?

如果有人点击禁用的 TreeNode 会怎样。 如果它仍然折叠/展开,还是应该留在 state 中:

protected override void OnBeforeExpand (System.Windows.Forms.TreeViewCancelEventArgs e)
{
    if (e.Node is TreeNodeThatCanBeDisabled treeNode)
    {
        // cancel expand if not enabled:
        if (!treeNode.IsEnabled)
           e.Cancel = true;
    }
}

类似崩溃?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM