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

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

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

我如何在运行时通过单击按钮而不在表单上出现PropertyGrid的方式来显示此编辑器窗口?

尽管我已经从该描述符中获得了PropertyDescriptorUITypeEditor,但我不知道在调用ITypeDescriptorContext以显示编辑器时要调用什么以获取IServiceProviderUITypeEditor.EditValue的实例。

这与为属性Building Windows Forms Controls and Components with Rich Design-Time Features构建自定义UITypeEditor有关。在这种情况下,我已经配置了所有这些功能,并且都可以很好地工作,因此我只想在运行时调用编辑器窗口。

最佳答案

如果您设法实现了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)

10-05 18:01
查看更多