locked
PropertyGrid won't set array values RRS feed

  • Question

  • I'm displaying a class using a PropertyGrid, where one of the properties of the class is a byte[] array.

    The byte array shows up in the property grid as an expandable node, and it displays the proper values found in the array.  If, however, I change any of the array's values using the PropertyGrid, the property's "set()" method is never called.  I've confirmed this by placing a break point in the set() method, changing a value in the grid, then moving focus to another line in the grid.

    Am I missing some sort of editor attribute for the property?

     

    		public byte[] Power
    		{
    			get { return power; }
    			set
    			{
    				power = value;  // Breakpoint never occurs.
    			}
    		}
    		byte[]	power = new byte[ 20 ];
    


     

    Thursday, June 23, 2011 6:15 PM

Answers

All replies

  • Your Set accessor is not needed because one element is changing.  Your set accessor dumps an entire array and replaces it with a new one, but the property grid is only changing a single element.
    MCP
    Thursday, June 23, 2011 6:23 PM
  • Your Set accessor is not needed because one element is changing.  Your set accessor dumps an entire array and replaces it with a new one, but the property grid is only changing a single element.
    MCP


    I think I understand, but that leads to the question of how do I get the PropertyGrid to change single elements in the array ?

    There is some validation I do in my Set ( not shown ) and I would like that to occur each time a single element is changed in the PropertyGrid.  So, which one would I validate?  Doesn't matter.  I'd iterate through all of them and, for example, clamp the values.

    If there's something I'm still missing, please expound.

     

    Thanks.

    Thursday, June 23, 2011 8:52 PM
  • OK, I think I understand a bit more now.  It seems the property grid is changing the elements through a reference to the array, and now your statement that the Set accessor is not needed makes sense.  It will never get called ... by the property grid.

    To get my validation ( value clamping ) to work, I put it in the Get accessor and it works since the Get is called each time something changes.  Kinda strange that it calls the Get, but not the Set.

    Thursday, June 23, 2011 9:05 PM
  • The correct way would be to provide your own type converter.  I imagine the PropertyGrid control is using  the default type converter for arrays, which unfortunately I don't know which is.  But I guess it is not too complicated to create one by inheriting from ExpandableObjectConverter.
    MCP
    Friday, June 24, 2011 5:01 AM
  • I agree with webJose to suggest you use the custom type converter, and for more details about how to do this, you can ref my post:

    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/2038bcfe-4baf-434c-af39-17056daf3410

    And this sample code: http://www.codeproject.com/KB/vb/PropertyGridEx.aspx

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 27, 2011 4:07 AM
  • RWey,
    I am writing to check the status of the issue on your side. 
    What about this problem now? 
    Would you mind letting us know the result of the suggestions?
    best wishes,

    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 29, 2011 3:18 AM