考虑一个具有一些自定义UserControl
和Button
的表单。
在Visual Studio设计器中,您可以单击属性右侧的按钮(就像更改控件(如Font
或Image
)上的其他常用属性时一样),然后使用该属性的编辑器。
在运行时,如果已将PropertyGrid
添加到表单并将其指向此UserControl
,则也可以在运行时单击该复杂属性右侧的按钮,并获得相同的UITypeEditor
对话框。
如何在运行时通过单击按钮而不在表单上显示PropertyGrid
来打开此编辑器窗口?
虽然我已经从这个描述符中获得了PropertyDescriptor
和UITypeEditor
,但当调用UITypeEditor.EditValue
以使编辑器显示时,我不知道该调用什么来获得ITypeDescriptorContext
和IServiceProvider
的实例。
这与为属性构建自定义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)
。