DenysVuika / WPG

WPF PropertyGrid Control
Apache License 2.0
101 stars 47 forks source link

SubProperties are not refreshed when the Component of a PropertyItem is changed #13

Open ghost opened 13 years ago

ghost commented 13 years ago

If have a property on an object that is another object with sub properties, if that top level property is changed to another object the sub properties for the new object are not created. This may seem a bit confusing so here is a sample of what I mean:

I have three classes, assume they all correctly implement the INotifyPropertyChanged interface.

[TypeConverter(typeof(ExandableObjectConverter)]
public class Parent
{
  public object Child { get; set; }
}

[TypeConverter(typeof(ExandableObjectConverter)]
public class Child
{
  public string Name { get; set; }
}

[TypeConverter(typeof(ExandableObjectConverter)]
public class OtherChild
{
  public string OtherName { get; set; }
}

The SelectedObject of the PropertyGrid is an instance of type Parent, and the Child property is set to and instance of type Child. The type converter correctly creates the sub property of Name in the PropertyGrid. If somewhere in my code I change the Child property to an instance of type OtherChild...

parent.Child = new OtherChild();

I would expect the PropertyGrid to then update the sub properties and display the OtherName property in the PropertyGrid. However this does not happen unless you execute the Reload command, which seems a little over the top.

The other way I have found to fix this is by adding a couple of lines to the ComponentValueChanged method of the PropertyItem to re-create the sub properties.

private void ComponentValueChanged(object sender, EventArgs e)
{
  if (_value != null)
    _value = CreatePropertyValueInstance();

  OnPropertyChanged("PropertyValue");
}

I'm not sure if this is the correct fix, or if there are any other work arounds that I am missing. Any comments would be helpful.