I am trying to use MVVM for the first time. I have a Windows Phone (Mango) application that has a model class, presentation model class and xaml view page. I have controls (text fields) attached to a virtual machine, and the virtual machine is attached to a model.
Both the model and the view model implement INotifyPropertyChanged . The implementation I'm using is copied, so I can use it to try and figure out what I'm doing with INPC. Here is the code that is specified in both classes:
public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } }
I have one property in the model class that can be set manually (from a text field) or calculated (by changing one of the other properties). Let me name this result.
If I change one of the other properties and follow the steps, INPC is called both in the changed property and in the translation in the Model class, although PropertyChanged is null , so part of the code is skipped. Then, in the virtual machine, the property that was changed goes through this the INPC class (as part of the access set), and this time the PropertyChanged not null , so the PropertyChanged method is called. However, for a Result property, INPC is not raised (this property does not have an INPC called by another property accessor).
Here is one of the properties in the Model that is not a calculated property:
public int AgeSetting { get { return (int)GetValueOrDefault(AgeSettingKeyName, AgeSettingDefault); } set { AddOrUpdateValue(AgeSettingKeyName, value); Calculate(); } }
Here is the Calculated Value property in the Model.
public int PointsSetting { get { return (int)GetValueOrDefault(PointsSettingKeyName, PointsSettingDefault); } set { AddOrUpdateValue(PointsSettingKeyName, value); } }
From ViewModel, here are both properties:
public int Age { get { return person.AgeSetting; } set { person.AgeSetting = value; NotifyPropertyChanged("Age"); } } public int PointsAllowed { get { return person.PointsSetting; } set { person.PointsSetting = value; NotifyPropertyChanged("PointsAllowed"); } }
This has never been done before, I expected that INPC should go from the model class to the VM class to the user interface. This does not seem to work.
I know that the Result (calculate) property is changing, since I can go from the page and return, and the newly displayed value is correct. I just donβt know how to get from the calculated value in the model, the presentation model and before the presentation.
Thanks for any suggestions.