Click here to Skip to main content
15,892,809 members
Articles / All Topics

WPF and MVVM

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Oct 2014CPOL2 min read 8.9K   2   1
WPF and MVVM

One of the most interesting features of the .NET Framework I have been using in recent times is Windows Presentation Foundation which allows graphical interfaces to be described using XAML. This allows rich user interfaces to be put together simply and easily. One of the main areas of thinking on top of WPF is the Model-View-ViewModel approach to application design, which allows for the ‘separation of concerns’ or layering of an application design, with each of those 3 areas only dealing with a particular area.

By separating the 3 layers out, we can make testing our application through the use of unit testing frameworks such as NUnit much simpler, and this in turn should lead to more stable, better designed applications.

The 3 layers are the ‘view’ which is effectively just the UI description in XAML. There is some debate about how much code we can put in the view, but effectively it is just UI code and nothing else. There should theoretically be no data stored in the UI.

The ‘view-model’ is a class object that is purely in C# [or VB.NET] code and exposes the data and commands to the view. The view model is not coupled to the view, it has no knowledge of the view. Because of this, all features of the view model should be testable. We assign the view model to the view at run time through the use of a ‘data context’.

Models are all our other objects that do not directly provide data or services to the view.

Throughout the next few posts, I aim to very simply demonstrate the basics of WPF and MVVM, and then move to some more complex scenarios. I don't plan on doing this through making a full application, but rather focus on small areas of detail that you may be able to incorporate into your own projects, should you wish to do so.

There are also many other advantages of WPF, that are enabled through the use of frameworks such as Prism, which I will discuss when the time comes.

Finally, I would also like to say that I don't believe WPF is the answer to everything. For small, personal applications or throw-away applications that require a user interface, the investment in time and learning required for WPF may not be suitable.

Filed under: C#, CodeProject, WPF
Tagged: C#, Introduction, Model, MVVM, Net, View, Viewmodel, WPF, XAML

License

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


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionInteresting article about the WPF (Windows Presentation Foundation) Pin
Volynsky Alex25-Oct-14 11:05
professionalVolynsky Alex25-Oct-14 11:05 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.