I am working on a Windows Phone application that uses MVVM, but I am struggling with the implementation of MVVM for properties that need to be formatted from the model class for display in the view.
Let's say that I have a simple model class called Person.
public class Person {
public string Name { get; set; }
public DateTime Birthday { get; set; }
}
There is a list of objects Personthat are loaded from a locally saved file, and I want to show the list of persons on the list page, and then allow the user to click on a person to go to the details page, where there is more detailed information about this person.
On the list page, I want to show the person’s birthday as "Birthday: 2/22/1980" (where "2/22/1980" is a person, formatted Birthday)
On the details page I want to show a person’s birthday in a different format: "Eric birthday is 2/22/1980" (where "Eric" is a person Nameand "2/22/1980" a person is formatted Birthday).
Usually I just create a view model that formats correctly Birthday:
public class PersonViewModel {
private Person person;
public PersonViewModel(Person person) {
this.person = person;
}
public string BirthdayForList {
get {
return "Birthday: " + person.Birthday.ToString("ddd", CultureInfo.CurrentCulture);
}
}
public string BirthdayForDetails {
get {
return person.Name + " birthday is " + person.Birthday.ToString("ddd", CultureInfo.CurrentCulture);
}
}
}
To show these values in the user interface, I would create a collection of these view model objects (and bind them to the view):
ObservableCollection<PersonViewModel> Items
Now, what should I do when the person’s birthday is updated (somewhere on the details page) and make sure that it is Itemsupdated with updated properties BirthdayForListand BirthdayForDetails, at the same time, save Personlocally?
, Person, PersonViewModel , .
? ObservableCollection PersonViewModel? , -, NotifyPropertyChanged.
(: . , Birthday , , - .)