我正在使用MonoMac / C#并且有一个NSOutlineView,其中一些项目是可编辑的。因此,如果您选择一个项目然后再次单击它(慢速双击),该行的NSTextField将进入编辑模式。我的问题是,即使您右键单击该项目,也会发生这种情况。您甚至可以混合左键单击和右键单击以进入编辑模式。
这很烦人,因为你有时会选择一行然后右键单击它,然后在上下文菜单出现后一秒钟,该行进入编辑模式。
有没有办法限制其中的NSOutlineView或NSTextFields,只能使用鼠标左键进入编辑模式(除了在选择行时按Enter键)?
谢谢!
答案 0 :(得分:1)
我使用的方法是重写“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
答案 1 :(得分:0)
The answer above by @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 sleected, as long as the textfield it is set to
// 'Editable' (in the storyboard or programmatically):
nextResponder?.rightMouseDown(with: event)
}
}