简体   繁体   English

在NSOutlineView中显示某些项目的上下文菜单

[英]Show context menu for some items in NSOutlineView

I have a SourceList (NSOutlineView) and I want to display a context menu for some of the items. 我有一个SourceList(NSOutlineView),我想显示某些项目的上下文菜单。 Looking around I have found answers in Cocoa and Obj-C but I am trying to do this in MonoMac and C#. 环顾四周,我在Cocoa和Obj-C中找到了答案,但是我试图在MonoMac和C#中做到这一点。

It seems to me I need to do my own custom class which inherits from NSOutlineView and implement the method MenuForEvent. 在我看来,我需要做一个自己的自定义类,该类继承自NSOutlineView并实现MenuForEvent方法。 But when I try to replace my old, standard NSOutlineView with my own custom class, nothing shows up during runtime. 但是,当我尝试用自己的自定义类替换旧的标准NSOutlineView时,在运行时没有任何显示。 In my controller I call View.ReplaceSubviewWith(oldTree, newTree). 在我的控制器中,我调用View.ReplaceSubviewWith(oldTree,newTree)。

Do I need to do something else? 我还需要做其他事情吗? Or perhaps there's another way to accomplish this? 也许还有另一种方法可以做到这一点?

Instead of trying to swap the instance like that, you should be able to do it declaratively. 不必尝试像这样交换实例,您应该能够声明性地进行操作。

Make sure you 'Register' your custom outline view, eg: 确保您“注册”您的自定义轮廓视图,例如:

[Register("MySourceList")]
private class MySourceList : NSOutlineView
{
    // Need this constructor for items created in .xib
    public MySourceList(IntPtr handle) : base(handle)
    { }

Then, in the Xcode designer, select your outline view and specify the name you registered as the Custom Class for that object: 然后,在Xcode设计器中,选择大纲视图,并指定您注册为该对象的Custom Class的名称:

在此处输入图片说明

That way, when your view is created from the nib, the runtime will create the proper instance of your outline view in the first place. 这样,当您从笔尖创建视图时,运行时将首先创建轮廓视图的正确实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM