【发布时间】:2019-03-15 14:25:46
【问题描述】:
我需要将自定义属性值编辑器集成到PropertyGrid 控件中。代码如下
internal class VariableTypeEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
Type type = context.GetType();
PropertyInfo property = type.GetProperty("OwnerGrid");
IPlugIn plug = context.Instance as IPlugIn;
if (service != null && property != null
&& plug != null)
{
PropertyGrid owner = property.GetValue(context) as PropertyGrid;
if (owner != null)
{
Collection<VariableWrapper> variables = owner.Tag as Collection<VariableWrapper>;
if (variables != null)
{
VariableEditorForm editor = new VariableEditorForm();
editor.Value = plug.VariableName;
editor.Variables = variables.Select(o => o.Variable).Where(o => o.ValueType == plug.VariableType).ToArray();
editor.TopLevel = false;
editor.FormClosed += new FormClosedEventHandler((sender, args) =>
{
service.CloseDropDown();
});
service.DropDownControl(editor);
value = editor.Value;
}
}
}
return value;
}
}
我希望它以这种方式工作:
- 要更改属性的值,用户打开下拉菜单
VariableEditorForm(使用 PropertyGrid) - 用户选择
VariableEditorForm中列出的值之一,然后VariableEditorForm将自动关闭
现在可以了。但由于某种原因,EditValue 方法需要 2-3 秒才能返回其值。
为什么service.DropDownControl(editor) 关闭后没有立即返回?
【问题讨论】:
-
这是出乎意料的。您是否尝试过在另一个上调用 CloseDropDown (在 FormClosed 之前)?你试过调试吗?你确定没有异常发生吗(属性网格可以默默吞下很多错误)。
-
@SimonMourier - 我做了几个实验,发现 IWindowsFormsEditorService 等待 MainThread 空闲(例如从渲染主窗口)。
-
如果没有完整的复制代码,很难进一步挖掘。
-
我看到的唯一一件事是您没有处理表单,但我怀疑这是问题所在。
-
@TnTinMn 请看看我的回答。您对如何改进它有任何想法吗?谢谢。
标签: c# winforms propertygrid uitypeeditor