Creating ViewModel in Silverlight

In this blog we will learn how to create a ViewModel in Silverlight.

Below is the code to create a ViewModel in Silverlight.


public class PersonViewModel : IViewModel<Person>
{
public Person Context
{
get;
set;
}
public ICommand SaveContext
{
get;
private set;
}
public PersonViewModel()
{
SaveContext =
new UICommand(SaveContext_Executed);
Context =
new Person();
}
void SaveContext_Executed()
{
MessageBox.Show(
String.Format("{0} {1} {2} saved...",
Context.FirstName,
Context.MiddleName,
Context.LastName),
"Save Notification",
MessageBoxButton.OK);
}
}




public
class Person : ObservableObject
{
private string _firstName, _middleName, _lastName;
private float _salary;
public string FirstName
{
get { return _firstName; }
set
{
if (_firstName != value)
{
_firstName =
value;
NotifyPropertyChanged(
this, "FirstName");
}
}
}
public string MiddleName
{
get { return _middleName; }
set
{
if (_middleName != value)
{
_middleName =
value;
NotifyPropertyChanged(
this, "MiddleName");
}
}
}
public string LastName
{
get { return _lastName; }
set
{
if (_lastName != value)
{
_lastName =
value;
NotifyPropertyChanged(
this, "LastName");
}
}
}
public float Salary
{
get { return _salary; }
set
{
if (_salary != value)
{
_salary =
value;
NotifyPropertyChanged(
this, "Salary");
}
}
}
}