如何禁用树节点复选框?
How can I disable a treenode checkbox?
我正在使用 codeproject 中的 treeviewExtensions,它实现了一些 iEnumerable 递归,以便在检查节点时检查所有父节点和子节点。
接下来我想为我实现一种禁用特定节点的方法。我已通过 GUI 将颜色设置为灰色来指示节点不可用。但是,节点旁边的复选框仍然是 Enabled,没有 disable/enable 属性。我有什么办法可以解决这个问题?
如果 TreeNode 被禁用,您没有明确指定您想要什么。如果 TreeNode 被禁用,您只想要一些特殊的颜色,还是想要具有 Checked 状态和子节点的东西?
所以您想要一个具有某些特殊行为的 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;
}
而且我认为您应该创建一个 TreeNodeView,它可以一次启用/禁用多个 TreeNode,并且可以为您提供所有启用/禁用的节点。
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:您只想更改颜色吗?还是复选框?折叠/展开?
也许一个事件会告诉您:“嘿伙计,这个 treeNode 已被禁用”?
如果有人点击禁用的 TreeNode 怎么办。它应该仍然折叠/展开,还是应该保持现在的状态:
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;
}
}
类似于崩溃?
我正在使用 codeproject 中的 treeviewExtensions,它实现了一些 iEnumerable 递归,以便在检查节点时检查所有父节点和子节点。
接下来我想为我实现一种禁用特定节点的方法。我已通过 GUI 将颜色设置为灰色来指示节点不可用。但是,节点旁边的复选框仍然是 Enabled,没有 disable/enable 属性。我有什么办法可以解决这个问题?
如果 TreeNode 被禁用,您没有明确指定您想要什么。如果 TreeNode 被禁用,您只想要一些特殊的颜色,还是想要具有 Checked 状态和子节点的东西?
所以您想要一个具有某些特殊行为的 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;
}
而且我认为您应该创建一个 TreeNodeView,它可以一次启用/禁用多个 TreeNode,并且可以为您提供所有启用/禁用的节点。
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:您只想更改颜色吗?还是复选框?折叠/展开? 也许一个事件会告诉您:“嘿伙计,这个 treeNode 已被禁用”?
如果有人点击禁用的 TreeNode 怎么办。它应该仍然折叠/展开,还是应该保持现在的状态:
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;
}
}
类似于崩溃?