langstrom
langstrom

Reputation: 1702

Editing and firing PropertyValueChanged event for a List<string> in a PropertyGrid

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?

Upvotes: 2

Views: 1769

Answers (3)

SubqueryCrunch
SubqueryCrunch

Reputation: 1495

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

Upvotes: 0

Stefan
Stefan

Reputation: 12260

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)

Upvotes: 0

nevets
nevets

Reputation: 4818

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)?

Upvotes: 1

Related Questions