[英]How can I disable a treenode checkbox?
如果 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.