[英]Prevent editing of NSTextField by double right-click
我正在使用 MonoMac/C# 并有一个 NSOutlineView,其中一些项目是可编辑的。 因此,如果您选择一个项目,然后再次单击它(缓慢双击),该行的 NSTextField 将进入编辑模式。 我的问题是,即使您右键单击该项目,也会发生这种情况。 您甚至可以混合左键单击和右键单击以进入编辑模式。
这很烦人,因为您有时会选择一行然后右键单击它,然后在上下文菜单出现后一秒钟,该行进入编辑模式。
有没有办法限制 NSOutlineView 或其中的 NSTextFields,只使用鼠标左键进入编辑模式(除了在选择行时按 Enter 键)?
谢谢!
我使用的方法是覆盖“RightMouseDown”方法 [1, 2]。 在尝试在 NSOutlineView 和 NSTableCellView 中这样做但没有运气之后,诀窍是向下到层次结构中的较低级别到 NSTextField。 事实证明,NSWindow 对象使用 SendEvent 将事件直接分派到最接近鼠标事件的视图 [3],因此事件从最里面的视图到最外面的视图进行。
您可以在 Xcode 的 OutlineView 中更改所需的 NSTextField 以使用此自定义类:
public partial class CustomTextField : NSTextField
{
#region Constructors
// Called when created from unmanaged code
public CustomTextField (IntPtr handle) : base (handle)
{
Initialize ();
}
// Called when created directly from a XIB file
[Export ("initWithCoder:")]
public CustomTextField (NSCoder coder) : base (coder)
{
Initialize ();
}
// Shared initialization code
void Initialize ()
{
}
#endregion
public override void RightMouseDown (NSEvent theEvent)
{
NextResponder.RightMouseDown (theEvent);
}
}
因为“RightMouseDown”不叫base.RightMouseDown()
点击完全由逻辑的NSTextField忽略。 调用 NextResponder.RightMouseDown() 允许事件在视图层次结构中向上渗透,以便它仍然可以触发上下文菜单。
[1] https://developer.apple.com/library/mac/documentation/cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/rightMouseDown : [2] https://developer.apple.com/library/mac/documentation/cocoa/conceptual/eventoverview/HandlingMouseEvents/HandlingMouseEvents.html [3] https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ eventoverview/EventArchitecture/EventArchitecture.html#//apple_ref/doc/uid/10000060i-CH3-SW21
@salgarcia 上面的回答可以适用于原生 Swift 3 代码,如下所示:
import AppKit
class CustomTextField: NSTextField {
override func rightMouseDown(with event: NSEvent) {
// Right-clicking when the outline view row is already
// selected won't cause the text field to go into edit
// mode. Still can be edited by pressing Return while the
// row is selected, as long as the textfield it is set to
// 'Editable' (in the storyboard or programmatically):
nextResponder?.rightMouseDown(with: event)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.