Search code examples
c#winforms.net-2.0

PropertyGrid: getting PropertyValueChanged notifications from a CollectionEditor


The PropertyGrid control is very useful for editing objects at run-time. I'm using it as follows:

Form form = new Form();
form.Parent = this;
form.Text = "Editing MyMemberVariable";

PropertyGrid p = new PropertyGrid();
p.Parent = form;
p.Dock = DockStyle.Fill;
p.SelectedObject = _MyMemberVariable;
p.PropertyValueChanged += delegate(object s, PropertyValueChangedEventArgs args) 
{ 
    _MyMemberVariable.Invalidate(); 
};

form.Show();

As you can see, I'm using the PropertyValueChanged notification to figure out when to update _MyMemberVariable. However, _MyMemberVariable is a class that I didn't write, and one of its members is a Collection type. The PropertyGrid calls the Collection Editor to edit this type. However, when the Collection Editor is closed, I do not get a PropertyValueChanged notification.

Obviously, I could work around this problem by using ShowDialog() and invalidating _MyMemberVariable after the dialog is closed.

But I'd like to actually get PropertyValueChanged events to fire when collections have been edited. Is there a way to do that without modifying _MyMemberVariable (I don't have access to its source code)?


Solution

  • This isn't very elegant, but it solved the issue I was having when someone updates / changes the order of a collection from a property grid:

    propertyGrid1.PropertyValueChanged += (o, args) => PropertyGridValueChanged();
    propertyGrid1.LostFocus += (sender, args) => PropertyGridValueChanged();
    

    I listen to the LostFocus event when they click on something else. For my particular use case this solution is sufficient. Thought I'd mention it in case someone else finds this useful.