0

我为我的 TreeView 控件创建了一个自定义处理程序:

public class TreeViewOnlyLeavesSelectable : TreeView
{
    protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e)
    {
        //base.OnNodeMouseClick(e);
        if (e.Node.Nodes.Count == 0)
        {
            this.SelectedNode = null;
            this.Refresh();
            return;
        }
    }
}

问题是,它仍然选择非叶节点。我已经对其进行了调试,它正确执行了 if 语句,但在应用程序中仍选择了该节点。我究竟做错了什么?

4

1 回答 1

2

According to your code (e.Node.Nodes.Count == 0) you only want NON-leaf nodes to be selectable.

According to your description ("it still selects non-leaf nodes") you only want leaf-nodes to be selectable.

Other than that, consider overriding OnAfterSelect instead. That also works when the keyboard is used instead of the mouse.

The following code only allows leaf-nodes to be selectable.

protected override void OnAfterSelect(TreeViewEventArgs e)
{
    base.OnAfterSelect(e);
    if (e.Node.Nodes.Count != 0)
    {
        this.SelectedNode = null;
    }
}
于 2013-05-20T13:16:03.673 回答