This is my first practices in silverlight4 using MVVM pattern and Ria Service to only show data in the gridview
please walk with me in this steps
1-adding my framework
2-adding my ria service class DomainService1
3-in the silverlight project make the following
a)add a new folder and call it ViewModel
b)insert into the view model folder a c# class and Call it ViewModelBase
4-in the ViewModelBase Class make the follwing code
public class ModelViewBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
the previous class inherited from INotifyPropertyChanged and implement it
note)this INotifyPropertyChanged interface in the System.ComponentModel
5-Adding a new class for each page “each view” like EmployeePageViewModel
this calss will be the following code :
public class EmployeePageViewModel: ViewModelBase
{
private DomainService1 ds;
public EmployeePageViewModel()
{
ds = new DomainService1();
LoadOperation<Employee1> loadOperation = ds.Load<Employee1>(ds.GetEmployee1Query());
loadOperation.Completed += new EventHandler(loadOperation_Completed);
}
private void loadOperation_Completed(object sender, EventArgs e)
{
LoadOperation<Employee1> loadOperation = sender as LoadOperation<Employee1>;
if (loadOperation.HasError)
{
ListOfEmployees = null;
loadOperation.MarkErrorAsHandled();
}
else
ListOfEmployees = loadOperation.Entities.ToList();
}
private List<Employee1> _listOfEmployees;
public List<Employee1> ListOfEmployees
{
get { return _listOfEmployees; }
set {
_listOfEmployees = value;
RaisePropertyChanged(“ListOfEmployees”);
}
}
}
===========================================================================
in the EmployeePage.xaml
make the following
Add the name space
1) xmlns:vm=”clr-namespace:SilverlightApplication3.ModelView” which refer to the viewmodel in ur project
2)add the items source property as the following ItemsSource=”{Binding ListOfEmployees}”
==================================================================
in the EmployeePage.xaml.cs
but the following code
InitializeComponent();
EmployeePageModelView vm = new EmployeePageModelView();
this.DataContext = vm;
======================================================================
thats all
Happy programing