如何在运行时没有PropertyGrid的情况下打开复杂属性的属性对话框

考虑一个带有一些自定义UserControlButton的表单。

在Visual Studio设计器中,您可以单击属性右侧的按钮(就像更改控件(如FontImage )上的其他常用属性时一样)并使用此属性的编辑器。

在运行时,如果已将PropertyGrid添加到表单并将其指向此UserControl ,则还可以在运行时单击该复杂属性右侧的按钮,并获取相同的UITypeEditor对话框。

如何在运行时通过单击按钮而不在表单上显示PropertyGrid来获取此编辑器窗口?

虽然我从这个描述符中获得了PropertyDescriptorUITypeEditor ,但是在调用UITypeEditor.EditValue以显示编辑器时,我不知道要调用什么来获取ITypeDescriptorContextIServiceProvider的实例。

这与为属性构建自定义UITypeEditor有关: 使用丰富的设计时function构建Windows窗体控件和组件 。 在这种情况下,我已经配置了所有这些并且它都运行得很漂亮所以我只想在运行时调用编辑器窗口。

我已经设法实现了TypeDescriptor ,你差不多完成了。
这可能是一个开始:

 public class MyEditor: UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.DropDown; } public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); if (service != null) { SomeControl ctrl = new SomeControl(); ctrl.XYZ = ... service.DropDownControl(ctrl); value = ctrl.XYZ; } return value; } 

WinForms处理其余部分。

返回UITypeEditorEditStyle.Modal表单GetEditStyle并使用service.ShowDialog(ctrl) ,如果您有一个表单而不是一个控件。