How to determine when a PropertyGrid control is about to change a property of an object

I have a PropertyGrid extension control that allows users to set properties for some of my programmatic objects. These objects have an event that fires when one of their properties changes, and the PropertyGrid subscribes to this event so that it updates when the property changes. My problem occurs when a large number of objects are selected and the user sets a property on all objects at the same time. The control is loaded with Refresh () requests that take a long time (for example, setting a property to ~ 300 objects takes about 20 seconds with auto refresh enabled and only a fraction of a second when turned off).

I want the event handler not to update the grid while the property grid is in the process of setting a property, but unfortunately I couldn't find a way to detect when the grid "starts" and "stops" setting the property. I was hoping there would be methods or something that I could override, like ...

override void OnSetPropertyStart()
{
    suppressRefresh = true;
}
override void OnSetPropertyEnd()
{
    suppressRefresh = false;
}

      

Unfortunately, this does not seem to be the case. Is there any other way to detect when a property grid sets a property or otherwise achieves the same effect?

+2


source to share


1 answer


Is the type under your control? Can you add a couple of events FooUpdating

/ FooUpdated

? Another option would be to write a custom property model with TypeDescriptionProvider

, but I suspect this will be quite a lot of work. My first try was a before / after pair ...

Something like (updated to show 3.5 approach, see history for example 2.0):



class MyType : INotifyPropertyChanged, INotifyPropertyChanging
{
    public event PropertyChangedEventHandler PropertyChanged;
    public event PropertyChangingEventHandler PropertyChanging;

    protected void UpdateField<T>(ref T field, T newValue, string propertyName)
    {
        if (!EqualityComparer<T>.Default.Equals(field, newValue))
        {
            OnPropertyChanging(propertyName);
            field = newValue;
            OnPropertyChanged(propertyName);
        }
    }
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this,
            new PropertyChangedEventArgs(propertyName));
    }
    protected void OnPropertyChanging(string propertyName)
    {
        PropertyChangingEventHandler handler = PropertyChanging;
        if (handler != null) handler(this,
            new PropertyChangingEventArgs(propertyName));
    }
    private string name;

    public string Name
    {
        get { return name; }
        set { UpdateField(ref name, value, "Name"); }
    }
    private DateTime dateOfBirth;
    public DateTime DateOfBirth
    {
        get { return dateOfBirth; }
        set { UpdateField(ref dateOfBirth, value, "DateOfBirth"); }
    }
}

      

Then just handle the two events and enable / disable updates as needed.

+1


source







All Articles