C# TreeView.GetNodeAt() 单击图像的问题
当在 TreeView 中单击节点时,我使用 TreeView 上的单击事件来执行一些操作。 我通过使用鼠标坐标调用 GetNodeAt() 来获取单击的节点,如下所示:
private void TreeView_Click(object sender, System.EventArgs e)
{
MouseEventArgs mouseEventArgs = e as MouseEventArgs;
if (mouseEventArgs == null)
return;
// Get the node that is being clicked.
TreeNode node = this.GetNodeAt(mouseEventArgs.X, mouseEventArgs.Y);
// Do other stuff...
}
但是,GetNodeAt() 方法仅在单击节点标签时有效,当单击节点图像时,然后单击 GetNodeAt () 返回空值。 这有点烦人,因为单击图像时实际上选择了节点,但我无法找出它是什么节点。
有人有什么建议吗?
更新: 我收到了一些使用 SelectedNode 的建议。 我不能,因为它是在 Click 事件触发后设置的。 这实际上是在继承 TreeView 的控件中,它的作用是触发它自己的 Clicked 事件,但使用 TreeNode 代表的底层数据而不是 TreeNode 本身。
更新:事实证明,有人重写了我们代码中的 GetNodeAt() 方法,该方法引入了这种行为,但我没有意识到。 所以这个问题是无效的,GetNodeAt()方法没有问题。 为什么有人会这样做仍然是个谜。 :)
I'm using the click event on the TreeView to do some stuff when a node is clicked in the TreeView. I do this by getting the node that is click on by calling GetNodeAt() with the mouse coordinates, like this:
private void TreeView_Click(object sender, System.EventArgs e)
{
MouseEventArgs mouseEventArgs = e as MouseEventArgs;
if (mouseEventArgs == null)
return;
// Get the node that is being clicked.
TreeNode node = this.GetNodeAt(mouseEventArgs.X, mouseEventArgs.Y);
// Do other stuff...
}
However, the GetNodeAt() method only works when the click is on the node label, when the node image is clicked then GetNodeAt() returns null. This is a bit annoying since the node is actually selected when the image is clicked but I can't find out what node it is.
Do anyone have any suggestions?
Updated:
I've gotten some suggestions to use SelectedNode instead. I can't since it's set after the Click event is fired. This actually in a control that inherits TreeView and what it does is fire it's own Clicked event but with the underlying data that the TreeNode represents instead of the TreeNode itself.
Updated: Turns out that someone had overridden the GetNodeAt() method in our code which introduced this behavior, which I didn't realize. So the question is null and void and there is no problem with the GetNodeAt() method. Why someone would do this remains a mystery. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尝试过 BeforeSelect 或 AfterSelect 事件吗? 您可以直接从 TreeViewCancelEventArgs 获取选定的节点,然后在 Click 事件中使用它。
编辑:附加想法:我能看到的唯一问题是 BeforeSelect 事件实际上在 Click 和 MouseClick 事件之后触发。
另一个编辑:如果您需要在 Click 之前触发的事件,您可以使用 NodeMouseClick - TreeNodeMouseClickEventArgs 有一个 Node 属性。
Have you tried the BeforeSelect or AfterSelect Events? You can get the selected node straight from the TreeViewCancelEventArgs, then use it in your Click Event.
Edit: Additional Thought: The only problem I can see with this is that the BeforeSelect event actually fires after the Click and MouseClick Events.
Another Edit: If you need an event that fires before Click, you can use NodeMouseClick - TreeNodeMouseClickEventArgs has a Node property.
为什么不直接使用 TreeView.SelectedNode 呢?
Why not just use
TreeView.SelectedNode
?尝试:
编辑:被肖恩击败
Try:
EDIT: Beaten to the punch by Sean