none
Dependent Properties

    Question

  • Hi

    Given an application that uses an MVVM structure and a Class that has two properties that are strictly dependent on one another what is the best way of handling property changes on these properties?

    Our current implementation is as follows:

    1. the Account Class has properties accountid and accountname - clearly if one changes then the other also changes
    2. each property has a public property declared eg

     

    public string AccountId
    {
    get
    {
    return this._accountId;
    }
    set
    {
    this._accountId=value;
    OnPropertyChanged("AccountId");
    }
    }
    
    public string AccountName
    {
    get
    {
    return this._accountName;
    }
    set
    {
    this._accountName=value;
    OnPropertyChanged("AccountName");
    }
    }

     

    The problem is that wherever this class is employed we are forever having to right code such as 

     

    void Account_changed(object sender,System.ComponentModel.PropertyChangedEventArgs e)
    {
    if(e.PropertyName=="AccountID")
    {
    //do something
    }
    }

     

    with the if statement having to be included in order to avoid the 'do something' from firing twice. It seems to me that it would be preferable to have the Account_changed event fire only once. One way to do this is to, say, remove the OnPropertyChanged("AccountName"). Trouble is this then means that a TextBlock that binds to this property never gets alerted to the Name change.

     

    Can anyone offer any advice. Perhaps this is the 'correct' way of doing it...

     

     

     

    Tuesday, March 23, 2010 11:42 AM

Answers

  • Perhaps this is the 'correct' way of doing it.


    Yes, it is the correct way of doing it; the single event which is exposed by INotifyPropertyChanged is raised for all relevant properties, so you must use the "if".

    However if you get bored using INotifyPropertyChanged, you can pick one of your dependent properties and raise a dedicated event:

    public event EventHandler AccountChanged;

    public string AccountId
    {
        get { return _accountId; }
        set
        {
            if (_accountId == value)
                return;
            _accountId = value;
            RaisePropertyChanged("AccountId");
            RaiseAccountChanged();
        }
    }

    void RaiseAccountChanged()
    {
        var handler = AccountChanged;
        if (handler == null)
           return;
        handler(this, EventArgs.Empty);
    }


    Regards,

    Kasimier

    Tuesday, March 23, 2010 10:31 PM

All replies

  • Perhaps this is the 'correct' way of doing it.


    Yes, it is the correct way of doing it; the single event which is exposed by INotifyPropertyChanged is raised for all relevant properties, so you must use the "if".

    However if you get bored using INotifyPropertyChanged, you can pick one of your dependent properties and raise a dedicated event:

    public event EventHandler AccountChanged;

    public string AccountId
    {
        get { return _accountId; }
        set
        {
            if (_accountId == value)
                return;
            _accountId = value;
            RaisePropertyChanged("AccountId");
            RaiseAccountChanged();
        }
    }

    void RaiseAccountChanged()
    {
        var handler = AccountChanged;
        if (handler == null)
           return;
        handler(this, EventArgs.Empty);
    }


    Regards,

    Kasimier

    Tuesday, March 23, 2010 10:31 PM
  • Of course! Thanks for this - I've put it into practice and it works well.

    Wednesday, March 24, 2010 6:54 AM