【问题标题】:Propertygrid with multiple nested classes具有多个嵌套类的 Propertygrid
【发布时间】:2014-11-23 11:10:52
【问题描述】:

我一直在玩 PropertyGrids,将它们链接到类,并试图弄清楚 (如果可能的话) 我可以如何显示一个类,在一个 flat 结构(好像它们都在一个类中)

我有几个类,Foo 和 Bar,如下所示:

[Serializable()]
public class Foo
{
    private string m_Code;
    private Bar m_Bar = new Bar();

    [DisplayName("Code")]
    [Description("The Code of Foo")]
    public string Code
    {
        get { return m_Code; }
        set { m_Code = value; }
    }

    public Bar Bar
    {
        get { return m_Bar; }
        set { m_Bar = value; }
    }
}

[TypeConverter(typeof(ExpandableObjectConverter))]
[Serializable()]
public class Bar
{
    private string m_Name;

    [DisplayName("Name")]
    [Description("The Name of Bar")]
    public string Name
    {
        get { return m_Name; }
        set { m_Name = value; }
    }
}

我想了解/弄清楚是否可以修改数据在 PropertyGrid 中的显示方式。

具体来说,我想以平面/有组织的结构显示 Foo.Code 和 Bar.Name,就像它们都在同一个类中一样。

如果我使用不使用 TypeConverter,或者尝试使用 [TypeConverter(typeof(Bar))],那么我会在 PropertyGrid 中看到我的 Bar 类,但只有一行,并且无法编辑 Name 属性.

如果我像上面那样使用 [TypeConverter(typeof(ExpandableObjectConverter))],那么我可以看到 Bar 的展开箭头,在其下方 Name... 的属性看起来都不错。

但是,我都必须展开组,它会在右侧显示类名。显然我可以调用 PropertyGrid.ExpandAllGridItems();根据需要,但它也很丑。

如果我使用 [Category("Bar")],那么它会被包含在内,并根据需要组织在平面结构中,但仍然存在上述问题(展开级别和类名)

我在这里遗漏了一些明显的东西吗?还是我需要实现某种自定义类型转换器的情况?如果是这样,该怎么做?

编辑:

为了进一步说明,当我使用嵌套类时,它显示如下,因此在右侧显示类名,并展开箭头。

我想要如下所示的嵌套类/好像 Foo 只是一个具有代码和名称的类。

编辑2:

尝试实现类型转换器,这似乎显示正常,但现在我根本无法编辑该值(即 Bar.Name )

[TypeConverter(typeof(BarConverter))]
[Serializable()]
public class Bar
{
    private string m_Name;

    [DisplayName("Name")]
    [Description("The Name of Bar")]
    public string Name
    {
        get { return m_Name; }
        set { m_Name = value; }
    }
}

public class BarConverter : TypeConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        Bar _Bar = (Bar)value;
        return _Bar.Name;
    }

}

编辑 3:

类型转换器似乎可以用于获取值,但现在无法编辑 Bar.Name 值,它只是灰显:

当前类型转换器

public class BarConverter : TypeConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        Bar _Bar = (Bar)value;
        return _Bar.Name;
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        return base.ConvertFrom(context, culture, value);
    }
}

编辑#4

我认为这太难了。兜兜转转的时间太多了:(

【问题讨论】:

  • 我认为您真正想要的是自定义类型编辑器。见User Interface Type Editors
  • 谢谢,我现在正在努力解决这个问题。

标签: c# class nested propertygrid


【解决方案1】:

我认为您应该覆盖 ExpandableObjectConverter 而不是 TypeConverter

public class BarConverter : ExpandableObjectConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
             Bar _Bar = (Bar) value;
             return _Bar.Name;
        }
    }

【讨论】:

    【解决方案2】:

    我通过重写类的 ToString() 方法解决了类似的问题。找到解决方案here

    [Serializable()]
    public class Bar
    {
        private string m_Name;
    
        [DisplayName("Name")]
        [Description("The Name of Bar")]
        public string Name
        {
            get { return m_Name; }
        }
        public override string ToString()
        {
            return "Name you want to display";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-18
      • 2020-12-26
      • 2014-11-26
      • 2014-03-31
      • 2019-07-16
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      相关资源
      最近更新 更多