如果 TreeNode 被禁用,您没有明确指定您想要什么。你只想要一些特殊的颜色,或者如果 TreeNode 被禁用,你想要一些带有 Checked 状态和子节点的东西?
所以你想要一个有一些特殊行为的 TreeNode?p>
在您的 OO 课程中,您了解到,如果您想要与其他东西具有几乎相同行为的东西,您应该考虑从另一个派生。
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;
}
}
类似的崩溃?