如果未选择对象,如何在PropertyGrid中保持UITypeEditor可见?

时间:2014-02-09 03:15:38

标签: propertygrid uitypeeditor

我正在编写一个C#Windows表单程序,我在使用PropertyGrids和UITypeEditor时遇到了困难......我已经阅读了这篇文章:How to create custom PropertyGrid editor item which opens a form?这帮助我开始了。

这是我到目前为止的基本代码:

public FooClass Foo { get; private set; }

[Editor(typeof(FooEditor), typeof(UITypeEditor)),
TypeConverter(typeof(FooType))]
public class FooClass
{
    private bool _enabled;
    public bool Enabled
    {
        get { return _enabled; }
        set { _enabled = value; }
    }

    public void ShowWindow()
    {
        using (var test = new Form())
        {
            test.ShowDialog();    
        }
    }
}

private class FooType : ExpandableObjectConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        return "Click on the button to show the window";
    }
}

private class FooEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        var svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
        var foo = value as FooClass;
        if (svc != null && foo != null)
        {
            foo.ShowWindow();
        }
        return null;
    }
}

按预期工作:当在propertygrid中选择Foo对象时,我看到右边的“...”按钮,我点击并显示窗口:

enter image description here

我无法弄清楚即使在propertygrid中没有选择Foo对象时,如何让这个“...”按钮保持可见:

enter image description here

我想念一些简单的东西吗?

0 个答案:

没有答案