At Telerik, we are no strangers to development patterns and practices that emerge in the constantly growing and changing .NET ecosystem. As can be seen from planned increased support for the MVVM pattern in Silverlight 5, even Microsoft has accepted MVVM as the de facto standard for serious enterprise development with Silverlight and WPF. Even before this, the developers at Telerik have been working hard at ensuring that all of our components from the UI for WPF, UI for Silverlight and UI for WPF control suites support the MVVM pattern.
Taking RadGridView as an example, you can always take the easy way and allow RadGridView to generate columns for you based on your datasource, but sometimes we want more control over what is being displayed. Looking at the code below, we can see three key parts of MVVM in action with the UI for Silverlight/WPF:
Since we realize that so many different settings require binding to function in an MVVM-based environment, with every release we work to ensure that our controls are as MVVM-compliant as possible and will always support binding scenarios like the above. Two examples of this embedded support for the MVVM pattern are our inclusion of two necessary elements to our assemblies – ViewModelBase and a DelegateCommand Implementation. When using MVVM, one of the things we need to be sure of is ensuring the UI updates according to changes within the ViewModel, so ViewModelBase includes an implementation of INotifyPropertyChanged, allowing us to notify the UI of updates as follows:
DelegateCommand, on the other hand, is an implementation of the iCommand interface that allows for using commands between the View and ViewModel. Since MVVM discourages use of the code-behind file to provide for a more clear separation of concerns, we need some way to enable typical events to populate through to the ViewModel so that the users can interact with your application. As an example of this, we can bind a RadButton to a DelegateCommand from our ViewModel and hook it up to RadGridView using only Xaml and our ViewModel:
while in our ViewModel, we use a DelegateCommand to perform an action based on the passed parameter:
Telerik has also created a rich and powerful routed command framework that allows for further codeless interactions with our controls. One great example of this is the RadGridViewCommand, which allow you to fire off commands within RadGridView by use of simple element-to-element binding. To accomplish this, we can use a RadButton with both Command and CommandTarget being set:
While on our ViewModel, all that we need to do is initialize whichever RadGridViewCommands we are going to be using:
Telerik provides two of the basic MVVM gluing elements out-of-the-box: DelegateCommand and ViewModelBase classes located in Telerik.Windows.Controls.dll. We have also developed our own routed command framework, which enables some of the more complex controls to expose commands in order to expose their behavior.