0
Элемент управления верхнего уровня нельзя добавить в controlTell мне, пожалуйста, как показать ContextMenuStrip в PropertyGrid? Для этого я использую этот код:ContextMenuStrip в PropertyGrid
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);
Не удается воспроизвести, плюс, кажется, трудно поверить, так как ни FileEditor или MenuList равна нулю. Пожалуйста, покажите реальный код и полный стек. –
К сожалению, метод GetEditStype лучше писать следующим образом: –
public override UITypeEditorEditStyle GetEditStyle (ITypeDescriptorContext context) { return UITypeEditorEditStyle.DropDown; } –