我需要将一个自定义属性值编辑器集成到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
的DropDown(使用PropertyGrid)VariableEditorForm
中列出的值之一,之后VariableEditorForm
会自动关闭它现在有效。但由于某种原因,在EditValue方法返回其值之前需要2-3秒。
为什么service.DropDownControl(editor)
在关闭后不会立即返回?
我设法找到了解决方案。我不喜欢,但它的工作原理。代码如下。
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) =>
{
// disable main form rendering
User32.PostMessage(Process.GetCurrentProcess().MainWindowHandle, Messages.WM_DISABLE_RENDER, IntPtr.Zero, IntPtr.Zero);
service.CloseDropDown();
});
service.DropDownControl(editor);
// enable main form rendering
User32.PostMessage(Process.GetCurrentProcess().MainWindowHandle, Messages.WM_ENABLE_RENDER, IntPtr.Zero, IntPtr.Zero);
value = editor.Value;
editor.Dispose();
}
}
}
return value;
}
事实上,我的主窗体上有一个面板,它使用定时器来不断渲染自己。我注意到每次禁用该计时器时我的问题都会消失。所以没有更好的解决方案我决定将消息发布到我的主窗体以启用/禁用提到的计时器。
如果有人能为我的问题提供适当的解决方案,我将不胜感激。