PropertyGrid中的ContextMenuStrip

时间:2013-10-02 05:34:11

标签: propertygrid uitypeeditor

顶级控件无法添加到控件请告诉我,如何在PropertyGrid中显示ContextMenuStrip?为此我使用此代码:

public class SaveFileNameEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService FileEditor =
            provider.GetService(typeof(IWindowsFormsEditorService)) as
             IWindowsFormsEditorService;
            ContextMenuStrip MenuList = null;
            ToolStripMenuItem MenuItem = null;
            if (FileEditor != null)
            {
                MenuList = new ContextMenuStrip ();
                MenuItem = new ToolStripMenuItem("Add File...");
                MenuList.Items.Add(MenuItem);
                MenuItem = new ToolStripMenuItem("Save File ...");
                MenuList.Items.Add(MenuItem);
                MenuItem = new ToolStripMenuItem("Delete File ...");
                MenuList.Items.Add(MenuItem);

                FileEditor.DropDownControl(MenuList);
                MenuList.Show();
            }
            return value;
        }    
    }

但是我在行

上收到错误消息“顶级控件无法添加到控件”
FileEditor.DropDownControl(MenuList);

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。而不是

FileEditor.DropDownControl(MenuList);

Point MenuCoords = (System.Windows.Forms.Control.MousePosition);
MenuList.Show(MenuCoords);