Click here to Skip to main content
Click here to Skip to main content

MVVM Pattern Made Simple

By , 30 Oct 2012
 

Important Note

Friends, I would very much appreciate if you leave me a line or two in the comments section stating how you think this article can be improved and what other topics on MVVM you want me to cover. Thanks.

Introduction

As many companies are switching from WinfForms to WPF/Silverlight, several project managers recently asked me almost identical questions about MVVM:

  • What is MVVM Pattern?
  • What are the advantages of the MVVM approach?

The purpose of this article is to answer these questions and to explain the MVVM pattern in the simplest possible way.

I assume that the article readers have not had any previous exposure to the MVVM pattern but have some knowledge of WPF or Silverlight.

Time permitting, I plan to write more MVVM articles which will include comparison of different MVVM frameworks and introduce a new MVVM framework.

MVVM Overview

Model-View-ViewModel (MVVM) pattern splits the User Interface code into 3 conceptual parts - Model, View and ViewModel out of which the concept of the ViewModel is the new and the most exciting.

  • Model is a set of classes representing the data coming from the services or the database.
  • View is the code corresponding to the visual representation of the data the way it is seen and interacted with by the user.
  • ViewModel serves as the glue between the View and the Model. It wraps the data from the Model and makes it friendly for being presented and modified by the view. ViewModel also controls the View's interactions with the rest of the application (including any other Views).

ViewModel code can (and should) refer to the Model, but the Model classes (if separate from the ViewModel) should not be aware of the ViewModel. View should be aware of the ViewModel, but ViewModel should be not aware of the View. In the diagram above, the arrows are showing which MVVM part is aware of which.

Similar to the models in the older patterns, the Model within MVVM pattern is simply data coming from the service or the database. Quite often, the Model can be built to be part of the ViewModel. Because of that, in our samples, we'll skip the model and concentrate primarily on the ViewModel, the View and interactions between them.

WPF Concepts Related to the MVVM Pattern, Communications between the View and the ViewModel

MVVM pattern became possible because of the following new concepts that came into being as part of the WPF (and later Silverlight).

  • WPF Bindings - Each binding connects two properties (possibly on two different objects) so that if one of them changes, the other changes too.
  • WPF Data Templates, which convert non-visual data (ViewModel) into its visual representation (View).
  • WPF Commands (or Microsoft Expression Blend SDK interactivity behaviors) serve to pass the events from the Views to the ViewModels.

WPF Bindings, WPF Commands and MS Expression Blend SDK interactivity functionality provide the necessary plumbing for communications between the View and the ViewModel. Another method can be employed for such communications - C# events can be used to trigger a change within a View when something happens within its ViewModel. This method should be avoided whenever possible because it implies code behind within the view (to register an event handler). Instead, one should use the bindings (perhaps combined with the triggers) to pass information about changes within the ViewModel to the View.

Here is a diagram depicting different ways of communicating between the View and the ViewModel:

When looking at this diagram, one might ask why the Binding and C# Events arrows are pointing from the ViewModel to the View even though, as stated above, the ViewModel is not aware of the View. The answer is that the arrows on this diagram are pointing from an MVVM part that causes a change to the part which receives the change. The Bindings are set within the View and even though the ViewModel is not aware of the View, the changes to the ViewModel still propagate to the View via the Bindings. In case of C# events, the event handlers on ViewModel's events should also be set within the View's code behind, so that the View functionality will trigger a change when changes occur within the ViewModel.

WPF Bindings

Below is a short (and by no means comprehensive) overview of the WPF bindings needed for understanding the MVVM pattern. For more information on the bindings, try e.g. Data Binding Overview, or Data Binding in WPF or plenty of other resources available on the internet.

WPF binding connects two properties (called binding target property and binding source property), so that if one changes, the other changes too. The target property of a binding should always be a WPF dependency property (for the definition of a dependency property - check WPF tutorials). The source property of a binding should be either a dependency property or fire PropertyChanged event on the property change.

A simple binding sample is located under "BindingSample" solution. It displays student data (student first name and student grade point average) within its window:

The student data is represented by an object of class StudentData. One can see that StudentData has two properties: StudentFirstName and StudentGradePointAverage:

public class StudentData : INotifyPropertyChanged
{
    ...

    string _firstName = null;
    public string StudentFirstName
    {
        get
        {
            return _firstName;
        }
        set
        {
            _firstName = value;
            
            OnPropertyChanged("StudentFirstName");
        }
    }

    double _gradePointAverage;
    public double StudentGradePointAverage
    {
        get
        {
            return _gradePointAverage;
        }

        set
        {
            _gradePointAverage = value;
            OnPropertyChanged("StudentGradePointAverage");
        }
    }
}

These properties serve as the source properties. When changed, both these properties fire PropertyChanged event that carries the corresponding property name in its event argument. This allows the binding to update its target properties to the new values. StudentData plays the role of a ViewModel in this simple example.

The View is located within the MainWindow.xaml file. It simply displays the student data within a Grid panel. The important parts are the WPF bindings connecting the ViewModel's StudentData properties to properties of the TextBoxes within the View:

<TextBox Text="{Binding Path=StudentFirstName}"
         Grid.Row="1"
         Grid.Column="2"
         VerticalAlignment="Center" />
...
<TextBox Text="{Binding Path=StudentGradePointAverage}"
         Grid.Row="2"
         Grid.Column="2"
         VerticalAlignment="Center"  />

The TextBoxes' Text properties serve as the binding's targets.

Looking at the XAML code above, one might wonder how the Text property knows which StudentData object it should connect to. There are different ways to specify the source object within the binding, e.g., by setting Source, ElementName or RelativeSource properties of the binding. However, when those properties are not set, as in our sample, the Binding's source object is assumed to be specified by the DataContext property of the corresponding visual element. DataContext property has a feature that it propagates down the Visual Tree from ancestor to its descendants, unless changed explicitly or unless it hits a ContentControl or an ItemsControl on its path (to be explained later). So once the DataContext is set to the root element of the Visual Tree, e.g. the Window, most elements under that tree will have the same DataContext. MVVM pattern uses this frequently by setting the View's DataContext property to contain the ViewModel for that View.

In our case, the DataContext is set within MainWindow class constructor inside MainWindow.cs file:

this.DataContext = _studentData;

Correspondingly, the two TextBox elements within MainWindow.xaml file will have the same DataContext as their MainWindow ancestor and the bindings will connect the StudentFirstName and StudentGradePointAverage properties from _studentData object to the corresponding TextBox'es properties.

When source property and target property depend on each other, but are not equal (they can even be of different types) WPF Binding can employ the ValueConverters which transform the source property value into the target property value and vice versa.

DataTemplates

The above binding sample shows how to bind properties on a ViewModel to the corresponding properties within the View. However if we want to present multiple data objects of the same structure, it is better to factor out the presentation code as a DataTemplate.

SimpleDataTemplateTest solution contains a DataTemplate sample. The ViewModel is exactly the same as in the previous sample - represented by StudentData and it is attached to the View's DataContext within MainWindow class'es constructor. The View itself, however, is defined by the DataTemplate "StudentView" defined within the resources of MainWindow inside MainWindow.xaml file:

<DataTemplate x:Key="StudentView"
              DataType="this:StudentData">
  ...
</DataTemplate>

To display the StudentData object using the DataTemplate, a ContentControl element is employed:

<ContentControl Content="{Binding}"
        ContentTemplate="{StaticResource StudentView}" />

Its Content property is bound directly to its DataContext (since the binding path is not specified), making its Content to be set to the StudentData object. It's ContentTemplate property is set to the "StudentView" data template. The ContentControl "marries" the data and the DataTemplate producing the visual representation of the data.

Note that the Visual children/descendents of a ContentControl will have the ContentControl's Content property as their DataContext. Also note that DataContext does not automatically propagate to the Content property of the ContentControl. In order for the Content to be set to the ContentControl's DataContext, it has to be explicitly bound to it.

ContentTemplate is ideal to display a signed data item in a View, but if one has a collection of data, ItemsControl class can be employed instead to display the whole collection. For ItemsControl sample, see ItemsControlDataTemplateTest.sln solution:

In this solution, the ViewModel is represented by StudentListViewModel class which contains a collection of StudentData objects within its TheStudents property. ItemsControl element is used to display the collection of students:

<ItemsControl ItemsSource="{Binding TheStudents}"
             ItemTemplate="{StaticResource StudentView}" />

ItemsControl's ItemsSource property is bound to the collection of students and ItemTemplate property provides the DataTemplate for each element within that collection.

Note that the ItemsControl's children cells will have the corresponding element from the ItemsSource collection as their DataContext.

Using Commands for Passing the Events from the View to the ViewModel

Now, suppose that we have a button as part of the View. We want some action to be taken by the ViewModel when the button is pressed. This can be achieved by employing the WPF Commands or Microsoft Expression SDK interactivity functionality. I prefer the 2nd method, but using the commands is more wide-spread so, we'll discuss it here first.

Command objects are derived from ICommand interface which has two important methods: CanExecute method controls whether the corresponding control (e.g. Button or MenuItem) that is bound to this command is enabled or disabled, and Execute method specifies the action to be taken once the Button or MenuItem is clicked. Here, we are using the DelegateCommand which simply allows us to set arbitrary delegates for these two methods. These delegates are passed within the constructor of the DelegateCommand.

Command sample is located under CommandSample.sln solution. After starting the solution, press the button in the middle and you'll see the message with the popup:

CommandSampleViewModel represents the ViewModel for the sample. It contains a DelegateCommand as its TheCommand property. Its "CanExecute" delegate always returns "true" thus keeping the corresponding button always enabled. Its "Execute" delegate is implemented by the "OnExecute" function which simply shows a Message Box with "Command Executed" text. Please note, that in real life coding, you should not put any visual code within the ViewModels, even the calls to MessageBox.Show. Here I did it just for brevity and clarity sake so that the OnExecute function would perform something that the users can see.

The Button within the view file MainWindow.xaml binds its Command property to the TheCommand property of the view model...

<Button Content="Call Command"
        Width="100"
        Height="25"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Command="{Binding TheCommand}"/>

...which ensures that the command fires on the button click.

Using Microsoft Expression Blend SDK Interactivity Functionality for Passing the Events from the View to the ViewModel

The WPF commands have a shortcoming that they can only be attached to a few visual controls that have Command property, e.g. Button or MenuItem and they only fire on Click events. Microsoft Expression Blend SDK Interactivity functionality is much more generic in a sense that it allows to trigger corresponding methods on the ViewModel for almost any event that occurred on almost any visual element within the View.

Microsoft Expression SDK sample can be found in MSExpressionSDKInteractivitySample.sln solution. To make the Microsoft Expression Blend SDK interactivity functionality available to the project, we need to add two DLL files to the project's references: Microsoft.Expression.Interactions.dll and System.Windows.Interactivity.dll files. These files are part of Microsoft Expression Blend SDK but can be used separately from the rest of the SDK. They are located under MSExpressionBlendSDKDlls directory.

The sample shows how to trigger a ViewModel's method when the MouseEnter event is fired on a Rectangle object. After starting the sample, move the mouse pointer over the blue rectangle in the middle, and you'll see a MessageBox popping up.

The ViewModel is represented by MSExpressionBlendSDKSampleViewModel class. It has just one method MethodToCallOnMouseEnterEvent which pops up a message box (remember that you should not put any visual code including the MessageBox related code into the real life ViewModels. I put it here simply for the sake of clarity). The View is calling this method via Microsoft Expression Blend SDK's EventTrigger and CallMethodAction:

<Rectangle x:Name="TheRectangle" 
           Fill="Blue"
           Width="100"
           Height="100"
           VerticalAlignment="Center"
           HorizontalAlignment="Center">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseEnter"
                        SourceObject="{Binding ElementName=TheRectangle}">
            <se:CallMethodAction MethodName="MethodToCallOnMouseEnterEvent"
                  TargetObject="{Binding Path=DataContext, ElementName=TheRectangle}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Rectangle>

Unlike the commands, Microsoft Expression Blend SDK functionality does not provide a way to disable a visual element on which they "listen" for events. It makes perfect sense, because they can be used with the visual elements that cannot be disabled, e.g., Rectangle in the sample above. If you want to provide an ability to control whether an element is enabled or not via the ViewModel, you can simply bind that element's IsEnabled property to some boolean property within the ViewModel.

MVVM Sample

While the samples above showed how to use different WPF components to connect the View and the ViewModel, this example puts everything together and shows the MVVM pattern in action.

The MVVM sample can be found under MVVMSample.sln solution. It allows to get a list of students from a mock server, to add or remove entries corresponding to the individual students, to modify individual student's properties and to save the list to the mock server:

The ViewModel for the whole application is represented by StudentListViewModel class. This class contains a collection of ViewModel objects of type StudentViewModel class. StudentViewModel class defines the ViewModel for the individual students.

When the sample is started "Save Students" and "Add New Student" buttons are disabled to force the user to load the student data from the mock server. Once the student data is loaded, those buttons get enabled. These button's enabled/disabled state is controlled by IsSaveStudentsActionEnabled and IsAddStudentsActionEnabled properties of StudentListViewModel class. The actions that need to be performed once any of these buttons are clicked are defined by the ViewModel's methods GetStudentsAction, SaveStudentsAction and AddStudentAction and are mapped to the button's click events using Microsoft Expression Blend Interactivity functionality.

ViewModel representing the individual students is defined by objects of StudentViewModel class, which has three properties FirstName, LastName and GradePointAverage. It also defines a method DeleteStudentAction used to delete the object from its parent collection. This method calls DeleteStudentEvent event which triggers the removal.

The corresponding Views are defined as the DataTemplates within StudentsViewResources.xaml resource dictionary located under XAMLResources folder. StudentView is the DataTemplate for a single student defined by StudentViewModel object and StudentListView is the DataTemplate for the whole application.

The MainWindow class contains only a ContentControl that converts the StudentListViewModel object to display as StudentListView.

MVVM Discussions

Different MVVM Flavors

All the MVVM samples presented above have their Views represented by the DataTemplates. I think DataTemplates are the best for representing the views because their usage provides the clearest separation between the visual and non-visual functionality. In some projects, however, people prefer to use UserControls as views. In my experience, UserControls or CustomControls should be avoided as much as possible within the MVVM pattern, since they are more heavy weight than the templates and have some code behind, which can easily degenerate into containing some business logic.

When to Use UserControls

There are, however, two important cases when building a UserControl or a CustomControl is required:

  • When you are building a brand new control with brand new capabilities. For example, WPF does not contain built-in charting functionality, so, when you build a charting control, you are forced to build a UserControl or a CustomControl that takes some data input and draws a chart based on it.
  • When you are forced to use the functionality of an existing control and that existing control does not have the required capabilities, or does not provide ways to bind to all the data, you will have to extend such control to provide the required capabilities and to fit it in as MVVM View's building block. Example of such a control would be a DevExpress GridControl which has great features and great performance but was not built with MVVM in mind and therefore requires a lot of adaptations.

In summary - do not use UserControls or CustomControls to put together the Views within MVVM patter - this is better achieved by the DataTemplates (in conjunction with the DataTemplateSelectors, ControlTemplates, Styles and StyleSelectors). Use User and Custom controls to create building blocks for the Views within MVVM pattern instead, when necessary.

Multiple Views and ViewModels within an Application

Based on the discussion above, here is the diagram of communications between different modules within a well-built MVVM application

Each one of the Views interacts only with its personal ViewModel. The ViewModels, however, interact with the rest of the functionality: other ViewModels, Models, Services, etc. Please note that the ViewModels still need to be as independent of each other as they can be, to adhere to the separation of concerns principle. The fact that some ViewModels need to communicate between each other does not imply that they have to know each other's exact types. They can communicate e.g. via common interfaces or an event aggregator (for an example of an event aggregator, see e.g. Prism for Silverlight/MEF in Easy Samples. Part 3 - Communication between the Modules).

Which View Properties should be Controlled by the ViewModel

Potentially the ViewModel can contain any types of non-visual object including the objects used only within the Views, e.g. Brushes or text label string, etc. For example, we could add BackgroundBrush property to the StudentViewModel class described above and bind our View's Background property to it. On the other hand, there is no reason for StudentViewModel to contain information about the background colors. The background brush can be specified very well by the View. Putting it into the ViewModel will only confuse potential users of the class. The rule of thumb is that the ViewModel should only contain the data displayed within the view and methods and properties that can be used for interacting with other ViewModels. Different brushes, colors, animations, changes of colors and shapes that do not affect any other Views can very well be handled by the View itself with the help of the WPF provided tools like Triggers StyleSelectors and DataTemplateSelectors.

Advantages of the MVVM Pattern

The major advantage of the MVVM pattern is that it provides the best separation of concerns: under MVVM, View is in charge of the visual representation while the non-visual ViewModel is in charge of all of the interactions with the rest of the software including the Model, the Services (usually via the Model) and the rest of the ViewModels. All the MVVM advantages derive from this feature:

  • Flexibility and Customization - Different Views can be used with the same ViewModels allowing completely different visual representations of the same functionality depending on what different customers want.
  • Re-use - Because of the separation of Visual and non-Visual functionality, both the Views and the ViewModels have higher potential for re-use than if the Visual and non-Visual functionality were mixed, since e.g. a non-visual functionality usually would not be able to make use of a functionality that contains a Visual part to it.
  • Separation of the UI design and development - MVVM makes it possible to separate the work of the developer and the designer as clearly as possible: at first the developer can produce an application with the crudest possible GUI. Then the designer, using designer tools will be able to modify the GUI (the Views) without touching any non-visual code.
  • Testing - Writing automated tests for a Visual application is not easy. The View - ViewModel separation allows the ViewModel to be unit tested without the view. Since the ViewModel is in charge of the interactions with the rest of the application, such unit tests would cover the most important functionality while the View tests will only have to contain testing of the visual features, e.g. colors, animations, etc.

Some MVVM Pattern History

Model-View-ViewModel (MVVM) pattern evolved together with WPF as a new way of designing and building Visual applications.

It came naturally out of new programming concepts developed as part of WPF such as Bindings and DataTemplates. MVVM pattern was first introduced by Martin Fowler in Presentation Model and John Gossman in Model-View-ViewModel (MVVM) pattern. Since then, a lot of other people wrote about this pattern - the most famous article is probably by Josh Smith in WPF Apps With The Model-View-ViewModel Design Pattern.

Conclusion

The purpose of this article is to present the MVVM pattern to people who have some knowledge of WPF or Silverlight but did not have any previous exposure to the MVVM. I'd love to hear from you about possible ways of improving clarity of the presentation.

History

  • Nov. 22, 2011. Fixed some errors in the source code. Hat tip to user M.Sepahvand for noticing them.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Nick Polyak
Architect AWebPros
United States United States
Member
I have 15 years of experience developing enterprise software, starting from C++ and Java on UNIX and moving towards C# on Windows platforms.
I am fascinated by the new .NET technologies especially WPF, Silverlight and LINQ.
Recently I decided to make a move and start my own contracting consulting and mentoring company AWebPros.
I can be contacted via my web site awebpros.com or through my blog at nickssoftwareblog.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerRe: Model vs ViewModelmemberMember 99731269 Apr '13 - 10:30 
D'Oh! | :doh:
Ok, I think I get it now....   Sigh | :sigh:
 
I was reading Josh Smith's article WPF Apps With The Model-View-ViewModel Design Pattern at http://msdn.microsoft.com/en-us/magazine/dd419663.aspx (linked from this article) Smile | :)
 
In that example, the CustomerViewModel exposes certain Model data (from the Customer class), plus:
- Validation
- SaveCommand
- Selection
- Uses resource strings
Cool | :cool:
GeneralRe: Model vs ViewModelmemberNick Polyak13 Apr '13 - 15:58 
Exactly. As I said above View Model = Model + (View related functionality).
Regards
Nick
Nick Polyak

AnswerRe: Model vs ViewModelmemberNick Polyak13 Apr '13 - 15:57 
Yes, ViewModel is usually a Model + some view related functionality. While it is important to understand the distinction between the model and the view model, it is not always necessary to keep them physically them apart. This article is dedicated primarily to the interaction the view and the view model, so I left the model out of the pattern.
Regards
Nick
Nick Polyak

QuestionBlendedmemberMember 99731269 Apr '13 - 9:12 
Since you asked for feedback on how "this article can be improved" I offer the following:
 
This is great introduction to MVVM and indeed, as stated, it is good for folks with some WPF experience. Plus there are links to other articles too. Smile | :)
 
I would have prefered the article not use Blend. I do not use Blend on my project so including it in this introduction is unfortunate (I believe).
 
I would have liked to have seen a pure example and then Blend included as one of the different MVVM extensions/frameworks. There seem to be several, so a pure example would help me see the base WPF clearly so I can measure other frameworks.
AnswerRe: BlendedmemberNick Polyak13 Apr '13 - 15:54 
Hi,
Blend SDK can be used and installed totally separate from the blend. I actually emphasize in the article that I am not using Blend, only a couple of dlls from Blend SDK.
The functionality Blend SDK provides is (from my point of view) better and more generic than the command based functionality available from the .NET libraries, this is why I am using it. If, for some reason, using blend SDK is not desirable on the project, such functionality can be easily implemented in a separate DLL. I actually did it once myself.
Regards
Nick
Nick Polyak

SuggestionNick, when will you post to code Project your next article about MVVM in WPF?memberEugene_Buzin6 Apr '13 - 2:56 
Hi, Nick.
As I wrote this your article is very nice. But you wrote that you planed to present more articles about MVVM pattern for WPF applications development. When will you post them here to Code Project?
 
Sincerely yours Eugene Buzin.
GeneralRe: Nick, when will you post to code Project your next article about MVVM in WPF?memberNick Polyak6 Apr '13 - 19:55 
Hi Eugene,
At this point my interests shifted a little bit. I plan to write a bunch of blog articles on implementing some WPF ideas outside of WPF. This might touch some MVVM points, even though it is a different topic in general. I plan to come back to MVVM though, at some point.
Regards
Nick Polyak

SuggestionWAS VERY INFORMATIVEmemberManu353519 Mar '13 - 19:26 
Hi Nick Polyak I was searching for a better explainantions on MVVM but i cudnt find it anywhere but here u have explained it in a user friendly way but you have explained it by giving so many choices for connecting ViewModel to View. that confuses a new reader about MVVM so stick on to one choice and explain them briefly ....no offence but i myself felt confusiing by reading the many ways you have given but then it was very useful for me now i have a clear picture of what MVVM is why is it?and for what for it is? and how to use them in our project and I am copying some of the content from ur notes for my future reference so please forgive me for using your research.....
 
Thanks again...Grt Job...
GeneralRe: WAS VERY INFORMATIVEmemberNick Polyak6 Apr '13 - 19:52 
Thanks Manu,
I tried to give my view of the subject.
Nick Polyak

Questionnice postmemberbilal7869911 Mar '13 - 8:45 
Nice post keep it up.
here is also a an mvvm application
http://codesmesh.com/address-book-mvvm-wpf-application-c-sharp/[^]
AnswerRe: nice postmemberNick Polyak6 Apr '13 - 19:51 
Thanks!
Nick Polyak

GeneralMy vote of 5memberAndre Pieterse14 Feb '13 - 19:34 
Excellent! I like the way you cover the core concepts about MVVM, I've learned alot from this article Smile | :)
GeneralRe: My vote of 5memberNick Polyak17 Feb '13 - 16:29 
Thanks Andre!
Nick Polyak

Questionexcellent articlemembervaishnaviav25 Jan '13 - 5:09 
Excellent article. Thanks
AnswerRe: excellent articlememberNick Polyak5 Feb '13 - 15:01 
Thank you!
Nick Polyak

GeneralMy vote of 5memberprateekfgiet24 Jan '13 - 18:44 
Nice article i an new for MVVM but now i have better understanding on MVVM
GeneralRe: My vote of 5memberNick Polyak5 Feb '13 - 15:00 
Thanks!
Nick Polyak

GeneralMy vote of 5memberRajeshKumar Sugumar21 Jan '13 - 16:54 
Its more helpful.Great Work
GeneralRe: My vote of 5memberNick Polyak5 Feb '13 - 14:59 
Thanks Rajesh!
Nick Polyak

SuggestionIt would be very nice if you continue a discussion about MVVM. [modified]memberEugene_Buzin17 Jan '13 - 20:37 
Thumbs Up | :thumbsup: Nick, this your article is very high-professionally written. It would be very nice if you continue the WPF MVVM topic and describe more complex and advanced WPF-MVVM-application example on C# with clearly defined Model, View Model and View parts. (I bag yor pardon but use of ICommand is preferable for me now. Because first I want to familiarize with WPF MVVM application development using ICommand interface. Unfortunately I'm not very strong in WPF now.) By the way, is there a special forum for WPF MVVM developers somewhere?
 
Have a great day.
 
Very truly yours Eugene Buzin.

modified 18 Jan '13 - 3:49.

GeneralRe: It would be very nice if you continue a discussion about MVVM.memberNick Polyak20 Jan '13 - 19:01 
Thanks Eugene,
I have several project, I want to pursue, one of them is definitely MVVM. Using ICommand is perfectly legitimate, but I think Expression Blend SDK provides a better way of connecting to the ViewModel functions. I do not know any MVVM forums, but there is plenty of information on the web.
Best Regards
Nick Polyak

QuestionVery good article.memberEugene_Buzin17 Jan '13 - 2:11 
Hi Nick. I'm Eugene Buzin from Russia. First of all I beg your pardon for my poor English because I havn't had English tolking practice. Your article is very nice. But I'm interested in the following moment in your article. You use the Microsoft Expression Blend SDK instead of ICommand interface. I'm going to develop WPF-application for trading on the stock-exchange market. There is tight restrictions for time in my application functionality. Practically it will be a real-time application which must process input data for milliseconds and even more fastly. So I'd like to know about time that is consumed by Microsoft Expression Blend SDK work. Is the using of Microsoft Expression Blend SDK in application is more slow (from the viewpoint of time consuming) than the using of ICommand interface?
AnswerRe: Very good article.memberNick Polyak17 Jan '13 - 2:54 
Hi Eugene,
Expression Blend SDK (as well as ICommand) are only used to pass information from View to View Model. Their functionality is not called within loops and so, it is not critical for performance. There will be no performance impact whether you use one or the other.
Regards
Nick Polyak

AnswerRe: Very good article.memberNick Polyak17 Jan '13 - 3:00 
BTW, please, vote for the article.
Thanks
Nick Polyak

GeneralMy vote of 5memberMember 866348212 Jan '13 - 8:50 
Excellent article for beginners..
GeneralRe: My vote of 5memberNick Polyak13 Jan '13 - 7:31 
Thanks!
Nick Polyak

GeneralMy vote of 5memberGerstone9 Jan '13 - 23:33 
very good explanation of MVVM. The single samples make clear what happens. It is the 10.article i read, but the first i understood all.
GeneralRe: My vote of 5memberNick Polyak10 Jan '13 - 3:14 
Thanks Gerstone!
Nick Polyak

GeneralMy vote of 5memberhari111r2 Jan '13 - 23:35 
very clear article
GeneralRe: My vote of 5memberNick Polyak8 Jan '13 - 2:53 
Thanks!
Nick Polyak

QuestionExplanation of MVVM is excellent!!!memberostashinskiy20 Dec '12 - 11:20 
Thank you,
Nick!!!
AnswerRe: Explanation of MVVM is excellent!!!mvpNick Polyak20 Dec '12 - 11:30 
Thanks David!
Nick Polyak

AnswerRe: Explanation of MVVM is excellent!!!mvpNick Polyak20 Dec '12 - 11:43 
BTW, please vote for the article. Thanks
Nick Polyak

GeneralOne of the best articles explaining MVVMmemberjcarter12121224 Nov '12 - 7:43 
Thank you for making me understand the foundations of MVVM in such a simple way with nice examples. Keep it up.
GeneralRe: One of the best articles explaining MVVMmvpNick Polyak24 Nov '12 - 13:48 
Thanks!
Nick Polyak

QuestionVery good articlememberStefanJG12 Nov '12 - 22:01 
I find the article very useful for learning the basics of the MVVM patern. I had an application which was kind of an model-view-controller app developed in WPF, but I now see the great potential for converting it to MVVM to make it easier to expand on it in future iterations of the program.
AnswerRe: Very good articlemvpNick Polyak19 Nov '12 - 22:39 
Thanks Stephan!
Nick Polyak

QuestionGreat Workmembergeoffhirst12 Nov '12 - 3:38 
Nick,
 
Very nice work. You asked for feedback on other areas to cover in relation to MVVM. I am currently working on 2 projects, where MVVM is prevalent through both. I am now needing to share some code across projects namely for database access. So, I am now looking at how a project might be broken up into dll's mainly to ease code sharing.
 
There are other reasons for doing this also, and I was thinking that by moving the viewmodels into their own dll, it would enable me to catch any MVVM breakages that might exist where messagebox may have been used for example.
 
Breaking up a WPF app into dlls is new to me, so I am interested on other peoples thoughts, is it the done thing? Or am I just creating a code smell that will become a nightmare later on?
thanks in advance
GH
AnswerRe: Great WorkmvpNick Polyak19 Nov '12 - 22:39 
Hi GH,
sorry for the delay answering you. There are different ways to break the project into dlls. One possibility, just as you've mentioned, is to place the view models into separate dlls. However,
even if the view models are in the same dll with some views, it is still usually possible to test them separately from the views.
I recommend to look at it from the utility point of view. If you see any code visual or non-visual that can be re-used across multiple projects, just factor it out into a separate dll.
In my experience I usually have some non-visual utility library for non-visual re-usable components and a visual utility library containing custom controls and visual utility functions. In most of my projects I was not striving to separate VMs from the views since VM functionality is usually tied to high level business logic of the application and is not very re-usable across multiple components.
Regards
Nick
Nick Polyak

GeneralA great presentationmemberJohn R. Dempsey8 Nov '12 - 7:43 
Great work. Your presentation was very well thought out.
GeneralRe: A great presentationmvpNick Polyak8 Nov '12 - 14:30 
Thanks John!
Nick Polyak

GeneralThanks - my vote of 5memberDiamondDave5 Nov '12 - 7:50 
Thanks Nick. I've been struggling with WPF and MVVM for a while now. Thats helped to get it clearer in my head! Smile | :)
Dave McRae
On a clear day you can see the back of your head.

GeneralRe: Thanks - my vote of 5mvpNick Polyak5 Nov '12 - 12:08 
Thanks Dave!
Nick Polyak

GeneralMy vote of 5memberGozzi855 Nov '12 - 5:10 
It's a powerful example that explain in a very good way how to use the MVVM pattern for a beginners like me
GeneralRe: My vote of 5mvpNick Polyak5 Nov '12 - 12:07 
Thanks!
Nick Polyak

BugWrong string used (PropertyChanged)memberKnightwalker30 Oct '12 - 4:52 
Please change:
OnPropertyChanged("FirstName");
to
OnPropertyChanged("StudentFirstName");
 
and
 
PropertyChanged("GradePointAverage");
to
OnPropertyChanged("StudentGradePointAverage");
GeneralRe: Wrong string used (PropertyChanged)mvpNick Polyak30 Oct '12 - 8:39 
Yes, you are right. Thanks!
Nick Polyak

GeneralGood!!memberCanisLupus8425 Oct '12 - 22:50 
Nice article! Now I'm understand the mvvm-pattern
GeneralRe: Good!!mvpNick Polyak27 Oct '12 - 16:41 
Thanks!
Nick Polyak

GeneralMy vote of 5membervenu gopal reddy m23 Oct '12 - 0:13 
Way you have explained is Excelent my brother.. IF possible please write one Article like this on SilverLight with MVVM...

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 30 Oct 2012
Article Copyright 2011 by Nick Polyak
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid