Powered by Blogger.

Saturday, March 1, 2014

Tutorial on MVVM with WPF

Here in this article we will discuss about MVVM pattern in WPF and one example how to implement this pattern.You can also check my previous article

-How to give Tooltips in WPF in Asp.Net ?

MVVM is nothing but Model view and viewModel.This is the best architecture for WPF.

A View is defined in XAML and should not have any logic in the code-behind. It binds to the view-model by only using data binding.

The View contains the visual controls that will be shown to the user.This is the UI.

Normally the View is only aware of the ViewModel but there might be cases in which it may make sense to expose the Model directly to it.

This is simply a class representing your data. It has no functionality but the ability to hold data. It is your data or classes that represent entities in your application.

This object of the MVVM pattern represents the interface between your business logic and your user interface.
This is the way you connect your model to a specific View.

Advantages of MVVM pattern in WPF:
1- A clean separation between application logic and the UI will make an application easier to test, maintain.

It improves code re-use opportunities.developer-designer can able to work on the same time.
2- During the development process, developers and designers can work more independently and concurrently on their components.

The designers can concentrate on the view, and if they are using Expression Blend, they can easily generate sample data to work with, while the developers can work on the view model and model components.
3- It is easy to redesign the UI of the application without touching the code because the view is implemented entirely in XAML.

Now we will see one small implementation,where we are binding the values from the Model class through viewmodel to a ComboBox which is present in our view.

Open your VisualStudio,Go to File->New->Project->select Visual C#->Windows->Select "WPF Application" from the template window and Give the name as "EASYMVVM"->Then click on "OK".

Then You will find some default file created that are Properties,References,App.Xaml and MainWindow.xaml.