PropertyGrid selection error when properties have identical DisplayName?

I have PropertyGrid

one whose selected object contains multiple c properties [DisplayName]

for "Speed", all in different categories (the property names in the code are, of course, all unique). I noticed that if I (for example) have speed # 3 selected and called PropertyGrid.Refresh()

, the selection will automatically move to Speed ​​# 1. What's more, the speed # 3 value will sometimes be displayed next to speed # 1. The situation is resolved as soon as I click on the grid and change the selection, but this is clearly undesirable behavior.

I am currently hacking this by adding different symbols \t

to DisplayName

to make them unique. This is an acceptable workaround since tabs are not actually displayed, but I would of course prefer not to.

Is there a rule that everyone DisplayName

should be unique or is this a bug PropertyGrid

?


Update. Since it is someone's responsibility to ask for a sample code, insert one of these in PropertyGrid

and then call Refresh()

on it from the timer every two seconds or so:

class Demo
{
    [Category("Cat1")]
    [DisplayName("Speed")]
    public int Speed1 { get; set; }

    [Category("Cat2")]
    [DisplayName("Speed")]
    public int Speed2 { get; set; }

    [Category("Cat3")]
    [DisplayName("Speed")]
    public int Speed3 { get; set; }
}

      

+3


source to share


1 answer


I don't think this is a bug, it is probably a function (with side effects :-). You can check the source of the property grid on the Microsoft site. The relevant part is presented in the GridEntry.cs code:

public override bool Equals(object obj) {
  if (NonParentEquals(obj)) {
    return((GridEntry)obj).ParentGridEntry == this.ParentGridEntry;
  }
  return false;
}

internal virtual bool NonParentEquals(object obj) {
  if (obj == this) return true;
  if (obj == null) return false;
  if (!(obj is GridEntry)) return false;
  GridEntry pe = (GridEntry)obj;

  return pe.PropertyLabel.Equals(this.PropertyLabel) &&
    pe.PropertyType.Equals(this.PropertyType) && pe.PropertyDepth == this.PropertyDepth;
}

      



As you can see, PropertyLabel is being used. If you follow the code a little more, the label will eventually use the DisplayName property (or name if the DisplayName attribute is not defined).

+1


source







All Articles