【问题标题】:Propertygrid displaying common properties for multiple objectsPropertygrid 显示多个对象的公共属性
【发布时间】:2012-05-10 22:52:12
【问题描述】:

propertygrid 不显示多个对象的可扩展属性的父值时遇到问题(使用 propertygrid.SelectedObjects)

[Browsable(true), TypeConverter(typeof(ExpandableObjectConverter))]
 public class MyNestedClass {
    private int a;
    private int b;

   [Browsable(true), 
    ReadOnly(false), 
    RefreshProperties(RefreshProperties.Repaint),
    NotifyParentProperty(true)]
   public int A {
      get { return this.a; }
      set { this.a = value; }
   }

   [Browsable(true), 
    ReadOnly(false), 
    RefreshProperties(RefreshProperties.Repaint),
    NotifyParentProperty(true)]
   public int B {
      get { return this.b; }
      set { this.b = value; }
   }


   public MyNestedClass(int a, int b) {
      this.a = a;
      this.b = b;
   }


   public override string ToString() {
      return this.a.ToString() + “; “ + this.b.ToString();
   }
}

这个类是另一个类 myClass 的一部分,其中 myNestedClass 被定义为其中的可浏览属性。

public class MyClass {
   …
   private MyNestedClass myNestedClassObject;
   …
   [Browsable(true),
    ReadOnly(false), 
    MergableProperty(true),
    RefreshProperties(RefreshProperties.Repaint)]
   public MyNestedClass MyNestedClassObject {
      get { return myNestedClassObject; }
      set { myNestedClassObject = value; }
   }
   …
}

当属性网格中只显示一个类 myClass 的实例时,一切正常。 属性网格显示:

…
MyNestedClassObject | 1; 2
   A                | 1
   B                | 2
…

如果我试图显示 MyClass 实例数组(通过 propertygrid.SelectedObjects),其中 myNestedClass 对象确实具有相同的 a 和 b 值,我只在 propertygrid 中看到类似的内容:

…
MyNestedClassObject |
   A                | 1
   B                | 2
…

我知道 propertygrid 旨在仅显示多个对象的公共属性,子属性 a 和 b 也是如此。但是为什么可扩展属性的所谓“类型转换器”(或父)行确实有一个空值部分,尽管子属性 a 和 b 对于所有对象都是相同的?

任何人都可以对此有所了解和/或可以帮助我解决这个问题吗?

在此先感谢

乔辰

【问题讨论】:

    标签: c# winforms propertygrid


    【解决方案1】:

    在对该问题进行更多研究后,我通过另一篇 stackoverflow 文章找到了解决方案:

    PropertyGrid mult. controls select, null exception on property set?

    The author had the same issue with expandable properties when multiple objects are selected where all properties are common to all instances. “typeconverter”(或父)行保持空白,另一方面显示子属性。

    他注意到在其实例显示在属性网格中的类中重写方法 Equals。 是的,就是这样。

    非常感谢 greggorob64 !

    致以诚挚的问候

    【讨论】:

    • 这真的很有趣。我在谷歌上搜索了一个与此非常相似的问题,发现你的回复直接链接到我的问题:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 2017-04-02
    相关资源
    最近更新 更多