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

Moving from Windows Applications to WPF

, 8 Jun 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

RakeshGunijan
Software Developer
India India
Hi,
I am Bachelor of Engg(Computer) from Mumbai University.
I am right now working in Singapore.

Comments and Discussions

 
GeneralWhere is the code PinmemberArfan Baig18-Mar-10 20:15 
GeneralThanks .. Good job done !! PinmemberArindam Tewary2-Oct-09 10:55 
GeneralRe: Thanks .. Good job done !! PinmemberRakeshGunijan2-Oct-09 17:41 
General[My vote of 2] need more work PinmemberDonsw14-Jun-09 11:09 
GeneralMy vote of 1 PinmemberJammer8-Jun-09 11:41 
GeneralMy vote of 1 Pinmemberameetj858-Jun-09 7:12 
GeneralMy vote of 1 PinmemberDpMozz8-Jun-09 5:17 
GeneralMy vote of 1 Pinmemberd1241222-Jun-09 0:28 
GeneralMy vote of 1 Pinmemberwegenerb25-May-09 5:42 
AnswerRe: My vote of 1 PinmemberRakeshGunijan25-May-09 17:16 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 8 Jun 2009
Article Copyright 2009 by RakeshGunijan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid