防止通过双击鼠标右键编辑NSTextField

3
我正在使用MonoMac/C#,拥有一个NSOutlineView,其中一些项目是可编辑的。因此,如果您选择一个项目,然后再次单击它(慢速双击),则该行的NSTextField将进入编辑模式。我的问题是,即使您右键单击该项,这种情况也会发生。您甚至可以混合左键单击和右键单击以进入编辑模式。
这非常令人恼火,因为有时您会选择一行,然后右键单击它,然后在上下文菜单出现后的一秒钟内,该行进入编辑模式。
是否有一种方法可以限制NSOutlineView或其中的NSTextFields,只使用左鼠标按钮(除了在选定行时按Enter键)进入编辑模式?
谢谢!

我也需要这个功能... 我最初的想法是在NSOutlineView的Delegate中覆盖ShouldEditTableColumn方法,并在不想编辑时返回false,但我还没有让它起作用。 - salgarcia
你解决了这个问题吗? - salgarcia
还没有。如果您能投赞成票,我会很感激,这样可以让它获得更多关注。谢谢。 - Christoffer Reijer
2个回答

1

@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)
    }
}

1
我所采用的方法是覆盖“RightMouseDown”方法 [1, 2]。在尝试在NSOutlineView和NSTableCellView中这样做失败后,诀窍是要深入到层次结构中更低的NSTextField中进行操作。事实证明,NSWindow对象使用SendEvent直接将事件分派到最接近鼠标事件的视图[3],因此事件从最内部的视图到最外部的视图进行传递。
您可以在Xcode中更改所需的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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接