好的,我终于能够找到如何实现这一点。
我试图创建一个CollectionEditor.CollectionForm
接近我需要做的自定义,但这不是正确的方向。
首先,创建一个包含用于编辑集合的 GUI 的常规 Windows 窗体。然后只需在表单中包含返回 DialogResult 的按钮/按钮。
现在完成我正在寻找的关键不是CollectionEditor.CollectionForm
我认为的正确方法,而是UITypeEditor
.
所以,我创建了一个继承自 UITypeEditor 的类。然后,您只需将其充实为:
public class CustomCollectionModalEditor: UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if (context ==null || context.Instance == null)
return base.GetEditStyle(context);
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService editorService;
if (context == null || context.Instance == null || provider == null)
return value;
editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
CForm CollectionEditor = new CForm();
if (editorService.ShowDialog(CollectionEditor) == System.Windows.Forms.DialogResult.OK)
return CollectionEditor.Programmed;
return value;
//return base.EditValue(context, provider, value);
}
}
需要注意的关键部分是函数GetEditStyle
和EditValue
. 负责触发您为编辑集合而创建的表单的部分位于EditValue
覆盖功能中。
CForm
是我在本次测试中设计的用于编辑集合的自定义集合编辑器表单。您需要获取与IWindowsFormsEditorService
关联的IServiceProvider
并简单地调用.ShowDialog(formVariable)
以IWindowsFormsEditorService
显示您设计用于编辑集合的表单。然后,您可以从表单catch
返回DialogResult
值并执行您需要的任何自定义处理。
我希望这可以帮助某人,因为我花了很多时间来确定合并它的正确方法。