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.
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.
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
If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.