2013-08-23 99 views
2

我正在使用MonoMac/C#並且有一個NSOutlineView,其中某些項目是可編輯的。因此,如果您選擇一個項目,然後再次單擊它(慢速雙擊),該行的NSTextField將進入編輯模式。我的問題是,即使您右鍵單擊該項目,也會發生這種情況。您甚至可以混合左鍵單擊和右鍵單擊進入編輯模式。通過雙擊右鍵單擊來防止編輯NSTextField

這很煩人,因爲您有時會選擇一行然後右鍵單擊它,然後在出現上下文菜單後的第二秒,該行進入編輯模式。

有沒有辦法限制它內部的NSOutlineView或NSTextFields,只用鼠標左鍵進入編輯模式(除了在選中該行時按Enter鍵)?

謝謝!

+0

我已經需要這個功能,以及...我的第一個想法是在NSOutlineView的Delegate中覆蓋ShouldEditTableColumn,並在不想編輯時返回false,但我還沒有完成這項工作。 – salgarcia

+0

你解決了這個問題嗎? – salgarcia

+0

不,還沒有。如果您可以投票給它以更多曝光,我將不勝感激。謝謝。 –

回答

1

我使用的方法是覆蓋「RightMouseDown」方法[1,2]。在NSOutlineView和NSTableCellView中嘗試這樣做並沒有運氣之後,訣竅就是在層次結構中更低級別地轉到NSTextField。事實證明,NSWindow對象使用SendEvent將事件直接派發到最接近鼠標事件的視圖[3],因此事件從最內層視圖繼續到最外層視圖。

您可以在Xcode更改所需的NSTextField在OutlineView使用這個自定義類:

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

0

The answer above by @salgarcia可以適於本地夫特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) 
    } 
}