3

I am using SimpleMVVM and have two separate classes (models), one using the second like this:

    public class Database : ModelBase<Database>
    {
        public String ServerName //{ get; set; }
        {
            get { return _ServerName; }
            set
            {
                if (_ServerName != value)
                {
                    _ServerName = value;
                    NotifyPropertyChanged(m => m.ServerName);
                }
            }
        }
        private String _ServerName = "MyTestServer";

        // other properties removed for brevity
    }

public class MyConfiguration
{
        /// <summary>
        /// Database information
        /// </summary>
        public Database DatabaseInfo
        {
            get { return _DatabaseInfo; }
            set
            {
                if (_DatabaseInfo != value)
                {
                    _DatabaseInfo = value;
                    NotifyPropertyChanged(m => m.DatabaseInfo);
                }
            }

        }
        private Database _DatabaseInfo = new Database();
}

When 'ServerName' is changed, the NotifyPropertyChanged(m => m.ServerName); command executes but NOT NotifyPropertyChanged(m => m.DatabaseInfo);

How do I make the NotifyPropertyChanged(m => m.DatabaseInfo); fire whenever one of the properties of Database changes?

BrianKE
  • 4,035
  • 13
  • 65
  • 115
  • If you need to track changes in subclass property you can just subscribe to it's OnPropertyChanged event. – Ivan Zub Aug 12 '14 at 15:36
  • I have tried both methods below but the DataBasePropertyChanged method is never called. Could this be due to the fact that these classes are both Models and not ViewModels? Can I implement a PropertyChanged on a Model? – BrianKE Aug 12 '14 at 16:19
  • why you need this at all? if you bind anywhere in xaml to {Binding DatabaseInfo.ServerName} your ServerName value will be updated. why you need a property change for your DatabaseInfo too? – blindmeis Aug 13 '14 at 09:09

2 Answers2

2

You can use the PropertyChanged event of the INotifyPropertyChanged interface to tell you when the child property changes.

In your MyConfiguration class:

public Database DatabaseInfo
{
    get { return _DatabaseInfo; }
    set
    {
        if (_DatabaseInfo != value)
        {
            _DatabaseInfo = value;
            NotifyPropertyChanged(m => m.DatabaseInfo);
            DatabaseInfo.PropertyChanged += DataBasePropertyChanged;
        }
    }
}

...

private void DataBasePropertyChanged(object sender, PropertyChangedEventArgs e)
{
    NotifyPropertyChanged(m => m.DatabaseInfo);
}

Please note that you will need to attach this listener each time that you change the DatabaseInfo property value. Also, note that if you just wanted to listen to one property, then you could have done this:

private void DataBasePropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "ServerName") NotifyPropertyChanged(m => m.DatabaseInfo);
}
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • I added this to MyConfiguration class but the DatabasePropertyChanged method never gets hit. I did change the method name to DataBasePropertyChanged. – BrianKE Aug 12 '14 at 16:05
  • I've updated my answer... try moving the handler attachment to the `DatabaseInfo setter`. – Sheridan Aug 12 '14 at 16:26
0
private Database _DatabaseInfo = new Database();

public MyConfiguration()
{
  this._DatabaseInfo.PropertyChanged += new PropertyChangedEventHandler(propChanged);
}

private void propChanged(object sender, PropertyChangedEventArgs e)
{
  // Now you can update the _DatabaseInfo.DatabaseInfo property to force the property changed event to fire.
}

Refer to the documentation here

INotifyPropertyChanged.PropertyChanged event

Jon
  • 3,230
  • 1
  • 16
  • 28