2

I'm trying to edit a List<string> with a PropertyGrid and it's not firing a PropertyValueChanged event when it's contents are modified.

I researched this and tried to use a custom TypeConverter class, but even when I get the editor to show and let me modify the values I can't get this event to fire.

I also tried using the below attribute and it pulls up the string editor, but this also doesn't fire the event on changes.

[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
    "System.Drawing.Design.UITypeEditor, System.Drawing, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]

I also tried using a UITypeEditor and overriding the EditValue method, but this never fires when editing the values.

public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
  MessageBox.Show("This never appears...");
  return base.EditValue(context, provider, value);
}

Is there a way to edit a List<string> and fire the PropertyValueChanged event?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
langstrom
  • 1,652
  • 11
  • 14
  • Did you try this: [PropertyGrid does not raise PropertyValueChanged event][1] [1]: http://stackoverflow.com/questions/15110594/propertygrid-does-not-raise-propertyvaluechanged-event – Jeff Anderson Aug 27 '14 at 16:18
  • Yes, this doesn't call the event when the property is changed through the PropertyGrid. – langstrom Aug 27 '14 at 17:16

3 Answers3

1

You should use BindingList<string> instead of List<string> to get PropertyValueChanged event fired.

Edit:

@LarsTech pointed out that ObservableCollection<string> is practically used in WPF but not winforms, and you should use BindingList<string> instead.

In short, BindingList supports more interfaces and more feature than ObservableCollection. Here are some advantages to go with BindingList:

  • BindingList implements IBindingList<T>, but ObservableCollection does not. IBindingList provides a whole bunch of functionality which can be used by the UI to provide a lot more things, look here for more details
  • BindingList implements ICancelAddNew that data binding mechanisms uses for cancelling the newly added item;
  • ObservableCollection does not listen to changes in its children but only to Insert and Remove events;

Point 2 and 3 full credits to: ObservableCollection(Of T) vs BindingList(Of T)?

Community
  • 1
  • 1
nevets
  • 4,631
  • 24
  • 40
  • In WinForms, BindingList is usually used. – LarsTech Aug 27 '14 at 18:22
  • 2
    This ended up working for me. I subscribed to the `ListChanged` event of a `BindingList`. This event fires one more time than the number of items in the list, so I had to account for that. Not exactly what I was looking for, but this got me up and going. Thanks! edit: Also, this doesn't fire the `PropertyValueChanged`, at least not that I could tell. – langstrom Aug 29 '14 at 13:57
0

As already stated by langstrom, the BindingList does doesn't fire the event PropertyValueChanged.

I used a simple and ugly workaround: I set the complete collection (it only has a few items) after adapting it:

CollectionValue=CollectionValue

(My aim was to get a red border around my custom PropertyGrid Editor for a ObservableCollection(Of String) if IDataErrorInfo provides some error for the edited property.)

Also see

https://wpftoolkit.codeplex.com/discussions/544080 (discussion)

https://wpftoolkit.codeplex.com/workitem/20977 (issue ticket)

Stefan
  • 10,010
  • 7
  • 61
  • 117
0

If your BindingList doesn't refresh after you modify a value with a PropertyGrid you can call yourBindingList.ResetBindings() to trigger the changes.

SubqueryCrunch
  • 1,325
  • 11
  • 17