views:

1365

answers:

1

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

+1  A: 

I did some research and even reproduced the problem, however the solution I found won't help you, but I'm hoping the information may help another person help you.

Here goes

The problem is easily reproducable by creating a new windows form project, adding a property grid and a listbox to the form and setting the listbox as the property grid's selected object.

//designer code excluded
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        propertyGrid1.SelectedObject = listBox1;

        propertyGrid1.PropertyValueChanged += delegate(object s, PropertyValueChangedEventArgs args)
        {
            MessageBox.Show("Invalidate Me!");
        };

    }
}

When editing the items collection of the listbox, the event will never fire, the reason is because the Items property returns a reference to the collection. Since adding items to the collection doesn't actually change the reference when the property never appears to change so the property grid.

The solution I tried would be to extend property grid, and update the logic that compares the two and checks if the data in the collection has changed and call the event. I tried this but the PropertyGrid had an internal class PropertyGridView that caused problems for me.

I hope this helps someone else figure out your problem.

-jeremy