Silverlight 4 now came up with the support of Command Binding. Using Command binding you can easily develop your Silverlight MVVM (Model-View-ViewModel) applications where your view will not know about data. In this article, I will describe you the Command binding feature in Silverlight 4 Step-by-Step.

In this example, I will create a Customer View which will load the customer information from the code behind once we click the button “Load Customers”. The button will have a command associated with it to load the records instead of loading on click event. This ensures that the view will have less code & no relation with the data.


Background

In earlier version of Silverlight, if you want to load something on the click of a button, you have to register the event in your view and then you had to call the appropriate method to load data. Lets say, as an example I want to load the customer information of my grocery shop when I click on a specific button. How will you implement this in Silverlight? The answer is simple. I will register a Click event of the button and then I will call the CustomerProvider to load the customer information in my view. It’s simple enough but do you agree that this scatters your view with the functionality to load the informations? Yup, this backend related calls are tightly coupled with your view. They know each other and if I want to give the same call from a different button, I have to register the click event for that button and then have to give a call. It looks a bit ugly in normal scenarios.


Now assume the MVVM architecture, where you will have the view to show the UI related stuffs, model as data and viewmodel to do my necessary staffs to call the provider to get the customer informations and store within the viewmodel. Your view will not have any information about your data. Once binded to the UI it will automatically load the informations. This will give you clean, maintainable code with separation of the view from the business logic.


Implementation of DelegateCommand

To implement the Command binding you have to create a DelegateCommand which implements the ICommand interface. The ICommand interface is available in System.Windows.Input namespace in the System.Windows.dll assembly. It defines the contract for commanding.
  • It has an EventHandler “CanExecuteChanged” which occurs when changes occur that affect whether the command should execute.
  • It has a method named “CanExecute” and returns boolean value true or false based on whether the command can be executed in its current state.
  • Another method named “Execute” which calls when the command is invoked.



What's Next?

Come and Read the Complete Article "Command Binding in Silverlight 4 (Step-by-Step)". There you will find more about this topic. The following points are covered there in depth:
    • Implementation of DelegateCommand
    • Implementation of ViewModelBase
    • Implementation of ViewModel
    • Implementation of UI (XAML)


Read the Complete Article @ DotNetFunda.com

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.