我需要将自定义属性值编辑器集成到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)打开DropDown VariableEditorForm
中列出的值之一,然后VariableEditorForm
将自动关闭现在可以使用。但是由于某种原因,EditValue方法需要2-3秒才能返回其值。
为什么service.DropDownControl(editor)
在关闭后没有立即返回?
答案 0 :(得分:0)
我设法找到了解决方案。我不喜欢,但是有效。代码在下面列出。
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;
}
事情是,我在主窗体上有一个面板,该面板使用计时器不断进行渲染。我注意到,每次禁用该计时器,问题都会消失。因此,我没有更好的隔离感,所以决定将消息发布到我的主窗体中以启用/禁用提到的计时器。
如果有人能为我的问题提供适当的解决方案,我将不胜感激。