Refresh PropertyGrid on object property change

Dec 1, 2010 at 9:38 PM

After an object is assigned to the "SelectObject" property, the property grid is displayed.  If a property of that object is changed somewhere else, the "Value" property from the PropertyGridItem class is triggered, but the value displayed on the propertygrid does not show the update.  I'm digging into the code to see why the propertygrid value is not updated, but wanted to see if anyone already knew how to make the propertygrid display the correct, changed, value.

Coordinator
Dec 2, 2010 at 12:36 AM

There is no way for the property grid to know that you changed the value of your object. To accomplish what you want, you have two choices.

1. Have the object that you expose in the property grid implement the INotifyPropertyChanged interface. The PropertyGrid listens for the changed event if your object implements that interface.

2. Rebind it to the PropertyGrid after you've made the changes.

Dec 2, 2010 at 2:00 AM
I did find an answer.... I put an event on PropertyGridItem and when the value changed, I triggered the event. On the grid side, I attached this event from every row item that got added, and each time it reprints the grid. Not the most glamorous, but it works. Incidentally my object does do the INotifyPropertyChanged interface, but that is good to change the value in the PropertyGridItem, but it does not update the display of the grid. The event seems to work.


From: "hyspdrt" <notifications@codeplex.com>
To: "chas bryant" <chas.bryant@gmail.com>
Sent: Wednesday, December 1, 2010 7:36:19 PM
Subject: Re: Refresh PropertyGrid on object property change [slg40:236778]

From: hyspdrt

There is no way for the property grid to know that you changed the value of your object. To accomplish what you want, you have two choices.

1. Have the object that you expose in the property grid implement the INotifyPropertyChanged interface. The PropertyGrid listens for the changed event if your object implements that interface.

2. Rebind it to the PropertyGrid after you've made the changes.

Read the full discussion online.

To add a post to this discussion, reply to this email (slg40@discussions.codeplex.com@discussions.codeplex.com)

To start a new discussion for this project, email slg40@discussions.codeplex.com@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com

Coordinator
Dec 2, 2010 at 2:23 AM

PropertyItem already implements INP and is how it's flows back up to the Editor, so not sure why you had to add an additional event. Maybe you have an old version??

Below shows where the String Editor listens to the INP Event from the PropertyItem which is called when the Value Property changes (note the call to OnPropertyChanged).

Are you using a Custom Editor that is maybe not listening for that event?

StringEditor Constructor as an example:

public StringValueEditor(PropertyGridLabel label, PropertyItem property)
	: base(label, property)
{
	if (property.PropertyType == typeof(Char))
	{
		if ((char)property.Value == '\0')
			property.Value = "";
	}

     --> property.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(property_PropertyChanged);
	property.ValueError += new EventHandler<ExceptionEventArgs>(property_ValueError);
PropertyItem Value setter Snippet:
if (propertyType.IsEnum)
{
	object val = Enum.Parse(_propertyInfo.PropertyType, value.ToString(), false);
	_propertyInfo.SetValue(_instance, val, (BindingFlags.NonPublic | BindingFlags.Public), null, null, null);
     --> OnPropertyChanged("Value");
}
else
{
	TypeConverter tc = TypeConverterHelper.GetConverter(propertyType);
	if (tc != null)
	{
		object convertedValue = tc.ConvertFrom(value);
		_propertyInfo.SetValue(_instance, convertedValue, null);
	     --> OnPropertyChanged("Value");
	}
	else
	{
		// try direct setting as a string...
		_propertyInfo.SetValue(_instance, value.ToString(), (BindingFlags.NonPublic | BindingFlags.Public), null, null, null);
	      --> OnPropertyChanged("Value");
	}
}
Dec 31, 2013 at 8:16 AM