.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

MVVM Pattern With Command in Silverlight - Part 2

Posted By:Mahadesh Mahalingappa       Posted Date: August 13, 2011    Points: 200    Category: Silverlight    URL: http://www.dotnetspark.com  

In this article I am going to add command to the earlier post MVVM Pattern With Commanding in Silverlight

MVVM Pattern With Commanding in Silverlight - Part 2 :

In this article I am going to add commanding to the earlier post  MVVM Pattern Implementation in Silverlight - part 1

Lets take a copy of the code created in the previous article . We would be doing some modifications to add the Commanding .

Lets start with the PeopleViewModel.cs

We need to add a new class say UpdatePersonCommand . I would place this class in the PeopleViewModel.cs  .
public class UpdatePersonCommand : ICommand

    public bool CanExecute(object parameter)
        if (parameter != null)
            CanExecuteChanged.Invoke(parameter, new EventArgs());
        return true;

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
        // We have some work to do here 


Let me also add my command :

public ICommand UpdateVM
        return new UpdatePersonCommand();
Let me now modify the MainPage.xaml.cs .

I will change the Button_Click handler as shown below :
private void Button_Click(object sender, RoutedEventArgs e)
    this.ViewModel.UpdateVM.Execute(this.Resources["ViewModel"] as PeopleViewModel);

What I do is just call the Execute on the Update Command which would modify my ViewModel .

As can be seen I pass the PageViewModel Instance in the execute method .

Nothing changes in the xaml code.

Just call the UpdatePerson in the Execute of the UpdatePersonCommand class .

public void Execute(object parameter)
    (parameter as PeopleViewModel).UpdatePerson();

Run the project. It works the same way .

Thanks . Happy coding .
 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend