Member 4550493 wrote:I read about MVVM model. Using MVVM model i can reduce the code behind.
What does it Mean?
The idea of the code-behind reduction is that you remove any code-behind that is not strictly to do with the presentation of the data - thus you separate the presentation completely, allowing the presentation to be changed independently of the rest of the application.
Member 4550493 wrote:As MVVM model is separating the view, Model and ViewModel.
What is the actual use of ViewModel.
I think of the viewmodel as being what the name suggests - a model of the view. if you're going to have a form that dispays a list, and allows the user to select an item from the list to "do something" with, then the view model will contain a property holding the list's elements, and functionality to "do something" with an element of that list.
You can write tests against the ViewModel without worrying about visual controls, and the ViewModel becomes ,as it were, the specification of the View.
Member 4550493 wrote:can't I bind the data from model itself rather than ViewModel?
Sure you can - but the idea of the viewmodel its to separate your presentation from dependency on your data - and vice versa. In order to present data to the user, you may need to manipulate that data in some way - and if you do this in the model in order to be able to bind to it, then you're tying your Data to the View. Fine in principal, until something changes
As an example. Say you have a Time stored in the DB and you ant to display it - you retrieve it into a model that contains a DateTime value.
You want to display the time as an analogue clock. So you need angles for the hands.
Easy, you say, add properties of HoursAngle, and MinutesAngle to your model and bind to them.
It works - but it's just Not Right, is it? What business has the model got in presenting angles? What if you now decide youwant to display the time as a percentage of the passage of a day? add more properties to the Model?
A better way is for the model to present the time as a datetime, and the ViewModel to provide bindable properties, where necessary, to provide the Gui representations.
(note this isn't necessarily a real world example - there are other ways of presenting an analogue clock than binding to an HoursAngle propery on a Viewmodel or a Model)
I waffled on for four articles about MVVM - which may help, or may not - see my sig for a link