1

I've been stuck on this problem for a bit, and I can't seem to wrap my head around where I am going wrong with this. I have a class UnitInfo with several properties which are used to populate a PropertyGrid in a winform. The properties:

public class UnitInfo {
    public byte[] data { get; set; } = new byte[5];
    public int serialNumber
    {
        get
        {
            return (int)data[0] & 0b0111_1111;
        }
        set
        {
            data[0] = (byte)(((int)data[0] & 0b1000_0000) | (int)value);
        }
        .
        .
        .
    }
}

The rest of the properties are similar. When I update any property but data in the PropertyGrid the changes to data are immediately shown. For example, if I change the serial number by manually typing a value into the PropertyGrid then data will update accordingly. However, the reverse isn't true. If I modify data in the PropertyGrid to change the value of the serialNumber then data will update but serialNumber will not.

I may be wrong in my understanding of PropertyGrid from reading through the docs, but shouldn't the Get method of all properties be called when one is changed? That's what seems to be happening when any property modifies data.

Why won't properties update when data is modified?

pavuxun
  • 411
  • 2
  • 15
  • This question is phenomenally unclear. Please edit your question to change the names of the class and the byte array property to different names so we can figure out what you're talking about. – Mark Benningfield Feb 15 '18 at 13:41
  • @MarkBenningfield sorry about that, I modified the names to make it (hopefully) more clear. – pavuxun Feb 15 '18 at 13:43
  • You are never updating `data`. The `set` method of data is never called. You are only changing the state of that byte array, the property never changes, it always stays the same 5 bytes (the just have different values). – nvoigt Feb 15 '18 at 14:11

2 Answers2

0

As far as I know the PropertyGrid does not update completely if you programmatically change dependent properties. In one of my projects I call PropertyGrid.Refresh() after programmatic changes for this reason. However I cannot prove this by citing MSDN.

So if you know in the setter of data that serialNumber depends on data (which PropertyGrid certainly does not now of), then call PropertyGrid.Refresh() in data.set (which requires you to write out the setter explicitely).

Maybe there is also a different way with events however.

Edit: see also this question (and answer): PropertyGrid doesn't notice properties changed in code?

oliver
  • 2,771
  • 15
  • 32
0

You should decorate the properties with the RefreshPropertiesAttribute to notify the PropertyGrid that it should handle updating the object.

[RefreshProperties(RefreshProperties.All)]
public byte[] data { get; set; } = new byte[5];

Do this for each property on the class that you want to be responsive to editing in the PropertyGrid control. Note that for this property in particular, you have to assign a new byte array, not just modify the values of the existing array.

Mark Benningfield
  • 2,800
  • 9
  • 31
  • 31
  • This works great! However, it requires that I click on properties in the `PropertyGrid` for them to be refreshed. Is there a way to tell the PropertyGrid to refresh all properties when `data` is modified? I've put the `RefreshProperties` attributes on all of my properties in the class – pavuxun Feb 15 '18 at 14:51
  • Try `RefreshProperties.Repaint` instead of `.All` – Mark Benningfield Feb 15 '18 at 14:54
  • I tried using `Repaint` but still the same behavior. It definitely works well enough though. Thanks! – pavuxun Feb 15 '18 at 15:04
  • What's wrong with yourPropertyGrid.Refresh()? See my answer. – oliver Feb 15 '18 at 16:40