How to cause bindings to be updated, particularly for derived values?

Posted by rrhartjr on Stack Overflow See other posts from Stack Overflow or by rrhartjr
Published on 2010-06-07T01:59:42Z Indexed on 2010/06/07 2:12 UTC
Read the original article Hit count: 239

Filed under:
|
|
|

I'm using some CLR objects that use the INotifyPropertyChanged interface and use the PropertyChanged function to update in WPF bindings.

Pretty boilerplate:

protected void RaisePropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Then the property:

    private double m_TotalWidgets = 0;
    public double TotalWidgets
    {
        get { return m_TotalWidgets; }
        set 
        { 
            m_TotalWidgets = value;
            RaisePropertyChanged("TotalWidgets");
        }
    }

Is there a better way to update a derived value or even the whole class?

Say I had a calculated value:

    public double ScaledWidgets
    {
        get 
        { 
            return TotalWidgets * CONSTANT_FACTOR;
        }
    }

I would have to fire ScaledWidget's PropertyChanged when TotalWidgets is updated, eg:

        set 
        { 
            m_TotalWidgets = value;
            RaisePropertyChanged("TotalWidgets");
            RaisePropertyChanged("ScaledWidgets");
        }

Is there a better way to do this? Is it possible "invalidate" the whole object, especially if there are a lot of derived values? I think it would be kind of lame to fire 100 PropertyChanged events.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET