Greetings readers. This post covers some basic concepts and application principle behind MVVM pattern.
I’m assuming you have some basic knowledge of what the MVVM pattern contains, for example you should know
- · What a view is (UserControl)
- · What a ViewModel is (inherits DependencyObject and acts as the datacontext for the view)
- · What an ICommand is (a delegate that can be bound to via the XAML)
Etc.
My interpretation of the pattern is that the code should follow these principles:
- 1. No Code-Behind (xaml.cs) file
- 2. Separation of concern
- 3. Decoupling of objects.
Let’s explore these principles
1. No Code Behind
This is a simple idea, that all of the properties and functions that need to be exposed to the view can be pre-calculated and presented to the view in the DataContext. The view needs to make absolutely no decisions at runtime.
Where some logic is completely view specific, for example converting a string “blue” into a SolidColorBrush, you should use a converter.
There are rare cases when a control only supports some type of function in codebehind. In these cases you should implement the codebehind, but you should also ask yourself why DataBinding is not supported, since the entire Silverlight Toolkit and many other controls do support this out-of-the-box. In some cases, you may even find instructions on creating an MVVM-friendly version of the control.
2. Separation of Concern
The ViewModel should:
· Get Data from the service layer, model layer, or data source
· Rework the data into any type of custom object that is needed for the final presentation
· Filter or reorder the data down to the relevant set for viewing
· Provide every integration point for user interaction as a Command
· Populate DependencyProperty attributes so that they can be bound to.
The View Should:
· Use DataBinding to display the data from the ViewModel
· Use Converters, if necessary, to convert properties to View-Specific types.


![]()
· As shown above, the properties supplied and/or requested may not be a 100% match, but this would not inhibit the controls from operating correctly as implemented.
3. Decoupling of Objects
The ViewModel should not:
· Access any properties of any of the View’s Controls or components or the View itself
· Call any methods that are defined in the View’s Controls or components or the View itself
· Throw an error if something in the View has changed (Build Error or Runtime Error)
The View Should Not:
· Throw an error if something in the ViewModel has changed (Build Error or Runtime Error)
Resist the urge to get a reference to your Sender in the Commands!
Until next time,
Chimp