Click here to Skip to main content
15,879,096 members
Articles / Desktop Programming / WPF

Moving from Windows Applications to WPF

Rate me:
Please Sign up or sign in to vote.
2.76/5 (17 votes)
8 Jun 2009CPOL3 min read 53K   36   10
Thinking cap for starting WPF

Introduction

I must say, WPF is a programming language which has some of the difficult syntax to start with. I was wondering what those bindings, routings are doing in the first place? All Windows programmers who are used to the layered (tiered) architecture will be wondering how to cope with these new concepts.

But then if you start learning WPF in conjunction with MVVM design pattern, it will be a fun. It segregates the business layer, data layer from the View. Yes, this is what the basic three tier architecture concept is all about!!! Looking at MVVM, one could easily say that it is tailor made for WPF.

Let’s try to move from Windows .NET 2.0 to WPF programming. We will first see a simple Employee management system developed using .NET 2.0. We will then try to develop the same using WPF with MVVM design pattern.

Visiting 3 Tiered Architecture

When developing professional software, we always want to keep the UI separated from the business component and the data. 

ThreeTieredDesign.jpg

We usually hold a business object behind every UI which mediates between UI and the model. The business objects strongly follow the “patterns and practices” to make the application scalable. The attached source code contains an example demonstrating a simple Windows application. It is having a Simple Windows Form with two ComboBoxes, one to select Employer and the other to select employee. Once you select an employee name, you can view his/her details.

On the WPF Way

Now get back to WPF. We have UI blessed with bindings. Then we always have those data objects which keep the editable data in memory (in memory representation of database). Now we can bind the data with UI. Bindings allow us to update data in both ways. We can update UI from data or data from UI.

DataBinding.jpg

Let's convert the above .NET 2.0 application to WPF. We can reuse the existing data layer. So we have the data. We will develop WPF application to display the information. We will follow the MVVM design pattern. We have XAML as View. We have the data layer as Model. It's time to pass this model information to View. We will design a layer (ViewModel) which will encapsulate model from View. The ViewModel will act as DataContext to View. We will use DataContext in XAML binding. So when you update the ViewModel from View (because of binding), view model will internally update the Model.

So let’s pull some tricks from the MVVM (or Josh Smith’s brainchild) to elicit WPF programming. 

MVVM.jpg

The above diagram depicts the details about how WPF applications can develop using MVVM design pattern. This article talks about developing a WPF application using MVVM, so let's remember a few guidelines for MVVM.

DeFacto for MVVM

  1. No code behind of XAML - absolutely clean view design
  2. View model should be passed as DataContext to UI View
  3. View model and Model should not access any UI/View component, object
  4. View model should inherit INotifyPropertyChanged. This is required to make ViewModel as Binding source
  5. View model should encapsulate model - This hides the intricacy of data storage from View

You Can Also Refer To The Links Below to Enhance Your WPF Skills

History

  • 21st May, 2009: Initial post
  • 22nd May, 2009: Minor updates
  • 6th June, 2009: Included an example to demonstrate .NET 2.0 application and its WPF equivalent

License

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


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

Comments and Discussions

 
GeneralWhere is the code Pin
Arfan Baig18-Mar-10 20:15
Arfan Baig18-Mar-10 20:15 
GeneralThanks .. Good job done !! Pin
Arindam Tewary2-Oct-09 10:55
professionalArindam Tewary2-Oct-09 10:55 
GeneralRe: Thanks .. Good job done !! Pin
User-Rock2-Oct-09 17:41
User-Rock2-Oct-09 17:41 
General[My vote of 2] need more work Pin
Donsw14-Jun-09 11:09
Donsw14-Jun-09 11:09 
GeneralMy vote of 1 Pin
Jammer8-Jun-09 11:41
Jammer8-Jun-09 11:41 
GeneralMy vote of 1 Pin
ameetj858-Jun-09 7:12
ameetj858-Jun-09 7:12 
GeneralMy vote of 1 Pin
DpMozz8-Jun-09 5:17
DpMozz8-Jun-09 5:17 
GeneralMy vote of 1 Pin
d1241222-Jun-09 0:28
d1241222-Jun-09 0:28 
Verry poor text. To less examples. Nothing about how to deal with Moving from Windows Applications
GeneralMy vote of 1 Pin
wegenerb25-May-09 5:42
wegenerb25-May-09 5:42 
AnswerRe: My vote of 1 Pin
User-Rock25-May-09 17:16
User-Rock25-May-09 17:16 

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.