如何在运行时打开不带PropertyGrid的复杂属性的属性对话框



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

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

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

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

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

这与为属性构建自定义UITypeEditor有关:构建具有丰富设计时功能的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)

最新更新