.NET/C# 中的 TreeView 双击行为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/473113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TreeView double-click behaviour in .NET / C#
提问by Ivan
I have a regular .NET Windows Formstreeview control. The nodes are setup like this:
我有一个常规的 .NET Windows 窗体树视图控件。节点设置如下:
Group
团体
---child
- -孩子
---child
- -孩子
If I double-click a collapsed Group node, it expands (as you'd expect) and the NodeMouseDoubleClick event is fired off, where my code does something if the selected node is NOT a group node.
如果我双击折叠的 Group 节点,它会展开(如您所料)并触发 NodeMouseDoubleClick 事件,如果所选节点不是组节点,我的代码会在其中执行某些操作。
The problem arises when the Group is located near the bottom of the treeview, so that when I double-click the Group node it would require the treeview to expand vertically to fit the child nodes into view. In such cases, if I double-click the Group node, by the time it expands and adjusts the treeview, my mouse cursor is over a child node (it had to push everything up), and that causes the NodeMouseDoubleClick to think the child node is selected, which causes very odd behaviour.
当组位于树视图底部附近时会出现问题,因此当我双击组节点时,它需要树视图垂直扩展以将子节点放入视图中。在这种情况下,如果我双击 Group 节点,当它展开和调整树视图时,我的鼠标光标位于子节点上(它必须将所有内容向上推),这会导致 NodeMouseDoubleClick 认为子节点被选中,这会导致非常奇怪的行为。
How can I get around this? Should I not be using NodeMouseDoubleClick or..?
我怎样才能解决这个问题?我不应该使用 NodeMouseDoubleClick 还是……?
I see it was also explained in the feedback report Problem with TreeView DoubleClick event after expanding/collapsing caused change of scroll.
我看到在扩展/折叠导致滚动更改后的 TreeView DoubleClick 事件问题反馈报告中也对此进行了解释。
采纳答案by BFree
The NodeDoubleClick
is fine, but instead of using the e.Node
, use this.treeView1.SelectedNode
.
本NodeDoubleClick
不过是罚款,而不是使用e.Node
,使用this.treeView1.SelectedNode
。
回答by tonyz
Double-clicking a TreeNode is a mouse gesture that is already "used" by the TreeView to collapse/expand nodes Microsoft doesn't push the UI standards as much as Apple does, and on some level it is disappointing that Microsoft has exposed NodeDoubleClick, because they are encouraging you to amend the TreeView with your own custom behavior. This can be misleading to end users, who expect common behavior from common controls.
双击 TreeNode 是一种鼠标手势,TreeView 已经“使用”了它来折叠/展开节点 Microsoft 并没有像 Apple 那样推动 UI 标准,并且在某种程度上令人失望的是 Microsoft 公开了 NodeDoubleClick,因为他们鼓励您使用自己的自定义行为修改 TreeView。这可能会误导最终用户,他们期望从共同控件中获得共同行为。
From Designing the User Interfaceby Ben Shneiderman, the first of Eight Golden Rules of Interface Design:
从设计用户界面由本·谢奈德曼,第一界面设计的八大黄金法则:
- Strive for consistency.
Consistent sequences of actions should be required in similar situations; identical terminology should be used in prompts, menus, and help screens; and consistent commands should be employed throughout.
- 力求一致性。
在类似情况下应要求采取一致的行动顺序;在提示、菜单和帮助屏幕中应使用相同的术语;并且应始终使用一致的命令。
Long story short, maybe you should not be using NodeMouseDoubleClick.
长话短说,也许您不应该使用 NodeMouseDoubleClick。