开发者

Disable certain nodes of a tree control

Hello i have a treeView c开发者_高级运维ontrol with checkboxes:

checkbox LEVEL1

  checkbox Child1
  checkbox Child2

checkbox LEVEL2

  checkbox Child1

I shloul not allow checking and unchecking of Child2 of Level 1 and Child 1 of Level 2?

is that possible in a tree View control?


The problem is, that a TreeNode doesn't have a Enabled state nor any event you can ask. So to emulate the Enabled state you could use the Tag property and save a boolean value there when you create each node.

Then you add an event to the TreeView.BeforeCheck and implement in some kind of this:

void TreeView_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
    var IsReadOnly = e.Node.Tag as bool?;

    if (IsReadOnly != null)
    {
        e.Cancel = IsReadOnly.Value;
    }
}


It's not possible as far as I know. But you can emulate it yourself:

Change the node color to gray:

treeControl.Nodes[0].ForeColor = Color.Gray;

And catch the click event:

private void treeControl_AfterCheck(TreeControl tc,
                                            NodeEventArgs e)
{
  if(e.Node.ForeColor == Color.Gray)
    e.Node.Checked = !e.Node.Checked;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜