How to get the modified properties from a Property grid

Grids for WPF Forum

Posted 13 years ago by Riana Rambonimanana
Avatar
Hi,

How do you get the modified properties from an object that was edited with the property grid in case of use of a custom DataFactory ?

We have implemented a custom DataFactory (a subclass of DataFactory) then provide it to the property grid and also associates the source object to the "SelectedObject" property.

All works fine and we can modify our object's properties as we want but now, it's time to save all modified properties and we just can't figure out how to retrieve modified properties.

Any advices will be welcome.

Thank you

Riana

[Modified at 01/04/2011 10:26 AM]

[Modified at 01/04/2011 10:26 AM]

Comments (5)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Riana,

That isn't something that is tracked by the PropertyGrid. In general, modified values are pushed back to the underlying object immediately (via IPropertyDataAccessors generated by the DataFactory).

Off the top of my head, you would either need to track the properties that are modified in a custom IPropertyDataAccessor which doesn't immediately push down any new values, or use a temporary/proxy object that you assign to PropertyGrid.SelectedObject.

For the former case, you'd have to build some central object that each custom IPropertyDataAccessor can register with when it's value is changed. Then you can check with that central object to see if any values need to be saved, and possibly save them.

For the latter case, you'd effectively be modifying a clone of the real object you want to edit. Then the clone would need to know if it has been changed since it was created. If it was, then you'd need to merge that back to the real object (or possibly replace it). You would also need to ensure to do a deep copy of any expandable properties, to ensure that you don't modify any values on the original object.


Actipro Software Support

Posted 13 years ago by Riana Rambonimanana
Avatar
Thanks for the advices.

I was hoping, I could leverage the "IsModified" property on the "IPropertyDataAccessor" interface ... what is it stands for so ?

In fact, my real problem is that I can't figure out how to retrieve the underlying property data accessors of the property grid. I tried to check the "Properties" property on the Property Grid but unfortunately, it is always empty (given the scenario I described in my first post).

My goal was to get all property data accessors then compare its DefaultValue and Value properties to check whether it is modified or not.

Your solutions will work I am sure but as they are a bit cumbersome, I am trying to find a more lightweight and less cost solution. If there isn't, I will take your solutions.

Thanks and sorry for my english, I am french actually :)

Riana
Posted 13 years ago by Riana Rambonimanana
Avatar
Hi,

I ended up by adding a property in my custom implementation of the DataFactory that is filled by the "GetProperties" method.

So that, I have access to all my property data accessors by doing something like : "MyPropertyGrid.DataFactory.MyPropertyDataAccessors",that contains all my "IPropertyDataAccessor". The "IsModified" property on the IPropertyDataAccessor is well set and it tells me when the IPropertyDataAccessor was modified or not

Thank you for your help.

Riana
Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Riana,

The IsModified tells the PropertyGrid if the property has been modified from it's default value. That's not to say the value hasn't been changed by the PropertyGrid though, which is what your original question implies. If an object has a property with a default value of "1", but has a current value of "2", then IsModified would be true even if it wasn't modified via the PropertyGrid.

The IPropertyDataAccessor.IsModified property is equivalent to the .NET PropertyDescriptor.ShouldSerializeValue method, when using a PropertyDescriptor. If IsModified is true, then the Reset method can be used to restore it to it's default value.

But as I mentioned, there isn't one place you can look for the IPropertyDataAccessors. They will technically be in the PropertyGrid.Items collection and potentially in the Items collection of any PropertyGridDataAccessorItems under the PropertyGrid. In addition, in the Items collections, you can have other data accessors (i.e. ICategoryDataAccessor or ICategoryEditorDataAccessor) that have one or more child data accessors. Even beyond that, PropertyGridDataAccessorItem can be created and destroyed by WPF as needed, so you may lose access to instances of IPropertyDataAccessor.

If you only have root level properties (i.e. none of the properties are expandable), then you can probably get away with simply looking in PropertyGrid.Items. But you'd still need to drill into any instances of ICategoryDataAccessor or ICategoryEditorDataAccessor.

Just so you are aware, your DataFactory will be called multiple times. The first time will be to get the properties of the selected objects. Then it will be called once per IPropertyDataAccessor (as returned from the first call) to see if that property has any sub-properties.


Actipro Software Support

Posted 13 years ago by Riana Rambonimanana
Avatar
Hi,

I perhaps express my needs not enough exactly but my original goal was just to retrieve modified properties from the property grid at a certain point in time, not to retrieve a history of modifications.

Until now, I just have root level properties so all works fine but I will take in consideration your remarks.

Anyway thanks for those precious informations !

Riana
The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.