Updating a PropertyGrid
Asked Answered
R

2

8

How can I have a property grid update automatically when the object in its SelectedObject property changes? I've tried implementing INotifyPropertyChanged in my class but the property grid does not actually show the new propertyies of the object in the background until I click on it.

I've tried subscribing to the PropertyChanged event of my object directly, and calling the Refresh() method of the PropertyGrid when it is envoked. But some of my properties are related. Meaning changing one property may evoke multiple PropertyChanged events. This seems to work fine, but I'm still wondering if there is a cleaner way of doing this through DataBinding. Also I'd like to avoid having the control Refresh multiple times after the user only updated a single property.

So is there a way to get the PropertyGrid to refresh from PropertyChanged events?

Rudelson answered 25/6, 2010 at 18:32 Comment(0)
A
0

I don't know if there's a built-in way to do it, but here's a suggestion if you want to avoid multiple calls to Refresh for related properties :

When a PropertyChanged event occurs, start a timer. If the event occurs again before the timer has elapsed, do nothing. In the Tick event of the timer, refresh the PropertyGrid and stop the timer

Avalokitesvara answered 25/6, 2010 at 19:38 Comment(1)
I know this answer is two years old, but I had the same question as the OP and I want to add to this answer that Timer has an AutoReset property. Meaning you don't have to stop the timer manually. :-)Arctogaea
S
32

Try adding the RefreshProperties attribute to each property that has dependencies:

[RefreshProperties(RefreshProperties.All)]

Now, each time this property changes - it will automatically refresh the other fields. This is a much cleaner approach than calling the property-grid's "Refresh()" each time.

Suckerfish answered 10/2, 2011 at 10:4 Comment(2)
Because it was posted about 8 months late.Population
Solved almost all issues. INotifyPropertyChanged requires multiple firing checks while this RefreshProperties sortcut actually bypass all that (slower, it seems on my machine but seems to never fail) Thank you !Flatware
A
0

I don't know if there's a built-in way to do it, but here's a suggestion if you want to avoid multiple calls to Refresh for related properties :

When a PropertyChanged event occurs, start a timer. If the event occurs again before the timer has elapsed, do nothing. In the Tick event of the timer, refresh the PropertyGrid and stop the timer

Avalokitesvara answered 25/6, 2010 at 19:38 Comment(1)
I know this answer is two years old, but I had the same question as the OP and I want to add to this answer that Timer has an AutoReset property. Meaning you don't have to stop the timer manually. :-)Arctogaea

© 2022 - 2024 — McMap. All rights reserved.