|
The two main advantages I see in using the ViewModel is that it allows you to write unit tests and separates the various concerns making for a cleaner implementation.
You can't unit test the code behind your view but you can unit test the ViewModel code. So by seperating your presentation logic from the view itself, you create a more reliable solution. Not sure if you are actually reducing the amount of code. It's just moved to the ViewModel
You can bind directly to the model but writing presentation logic (adding properties, sort routines etc) in the model creates performance issues and confuses other developers about the purpose of the model. So by using the ViewModel class to implement this logic, you can avoid these problems.
The idea behind MVVM is simple, finding good articles which step you through implementing MVVM can be hard to hard to find but MVVM fits nicely with the WPF/Silverlight data binding model.
"You get that on the big jobs."
modified on Saturday, May 14, 2011 7:34 PM
|
|
|
|
|
Member 4550493 wrote: I read about MVVM model. Using MVVM model i can reduce the code behind.
What does it Mean?
You are going to write code for any event (say click) in the view model rather than the code-behind for the view. This completely decouples your UI from the logic inside your event handler.
Member 4550493 wrote: What is the actual use of ViewModel.
The view model is going to act as the intermediary between your UI and the data mmodel.
Member 4550493 wrote: can't I bind the data from model itself rather than ViewModel?
Yes you can. But its cleaner to bind to the view model (though both approaches are equally valid and there are arguments for using each of them).
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
|
|
|
|
|
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
|
|
|
|
|
Hi,
My Project is devoloped with silverlight4.
In my Project i have used some telerik controls with version 3.
But, the problem with my application is it is taking too much memmory.
Actually my Application is showing records for perticular order no,
so, can u help me.
How can i reduce the memory?
Thanks,
Umesh Tayade
|
|
|
|
|
How do you know it's taking too much memory?
If it's your application don't you know how it's using memory?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi,
I had checked it in Task Manager.
Actually i havent devoloped this Application.
I am just going through it and i came to know that it is making full cpu usage.
can suggest me what are the possibilities of it?
How do i overcome it?
Thanks,
Umesh Tayade
|
|
|
|
|
I recently wrote an article about what Telerik did.
They have created something they named "Minifier" it takes your silverlight project and remove unused things and leave you with a optimized project. You should even try their "JustCode" it makes sure you have neat code that has less bugs.
http://www.dotnetfunda.com/articles/article1282-how-to-reduce-the-size-of-your-silverlight-application-with-telerik-minifie-.aspx[^]
Hope it helps
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
hi experts,
I am created a non client area Form and customize the Form's default border style border style, now i am facing a problem is that after i Maximize the window will hide the Window's taskbar
Please Help me
thanks in advanse
|
|
|
|
|
Hi,
another datagrid question :=)
I searched the web for this, but unfortunatelly did not find anything useful.
What I want to achieve is, if the user presses tab on the last visible column of a datagrid, it should automatically create the new line and open the first line for editing data.
So if I'm here:
Screenshot 1
and press tab the datagrid should do this:
Screenshot 2
Any ideas?
Kind regards,
Nico
|
|
|
|
|
Assuming the default TAB behavior (i.e. tabbing out of the final cell moves the focus from the grid to the next control) I would try providing a LostKeyboardFocus that inserts a row, and brings the focus back into the initial cell of that newly inserted row.
P.S. This may or may not work when there are no other controls on the page: I do not know if you get a LostKeyboardFocus+GotKeyboardFocus when the focus circles back to the same control.
|
|
|
|
|
Okay thanks for your answer
But I don't think this is the way I want to go. I thought about something like:
- Event "CurrentCellChanged" or something similar
- If my cursor was in the last visible column and the user pressed "TAB" then do something
But I had no luck putting this correctly together.
I have extreme problems finding out if the Current Cell is in the last visible column...
Kind regards,
nico
|
|
|
|
|
Yeah, that's why I suggested using the lost focus: the event figures out the last visible cell very reliably.
|
|
|
|
|
Ah okay then I didn't understand your first post correctly.
I'll give it a try...
|
|
|
|
|
In my wpf application, there are several textboxes to show the values of the data in the database.
The storedprocedure used, returns fields such as Notes and EnglishNotes as you see in xaml below.
The problem is that txtNotes always shows the value in txtEnglishNotes
I have checked and the storedprocedure does return the correct value for Notes and EnglishNotes but somehow the two textboxes show the value in txtEnglishNotes always.
Do you see why please?
Thanks
<TextBox Grid.Row="5" Margin="5" Name="txtNotes"
VerticalScrollBarVisibility="Visible" TextWrapping="Wrap"
Grid.ColumnSpan="2"
Text="{Binding Path=Notes}" MaxLength="100" />
<TextBox Grid.Row="12" Margin="5" Name="txtEnglishNotes" Grid.ColumnSpan="2"
VerticalScrollBarVisibility="Visible" TextWrapping="Wrap"
Text="{Binding Path=EnglishNotes}" MaxLength="100" />
|
|
|
|
|
arkiboys wrote: Do you see why please?
Nope, the posted snippet looks fine. Your problem is likely elsewhere.
|
|
|
|
|
Where can the problem be?
I have also checked the SP which returns the correct values for each field.
Thanks
|
|
|
|
|
Look for any code that refers to those text boxes... Maybe you're doing something with them in the code-behind that you forgot about. If you have trouble finding it, remove their names (WPF controls usually don't need names) and see what breaks.
Set breakpoints in the getters for the bound properties, and check their values when they're actually retrieved... If they're dependency properties, add a PropertyChanged handler to the definition and set a breakpoint to see if they're being changed.
|
|
|
|
|
HOw do I put "Set breakpoints in the getters for the bound properties" ?
I ask because at present I am not using field properties as in get or set...
So not sure where you mean to place breakpoint.
Thanks
|
|
|
|
|
When you read the values out of the database, where do you put them? There must be some piece of code that contains these properties. Put the breakpoints on the code that populates these properties - this is debugging 101; you need to learn this stuff rather than coming here and asking us to solve this without sight of your code.
|
|
|
|
|
This kind of debugging is what I have done already but have not spotted where the error is. I will continue again.
Thanks
|
|
|
|
|
Well, you have bound the controls to the Notes and EnglishNotes properties of some Class. Therefore, you need to set the breakpoint in the setter for these properties. Then when these properties get updated, you can check whether the expected values for these properties are being set. This sounds like a classic case of two properties getting set to the same value like this
foreach (DataRow dr in ds.Tables[0].Rows)
{
MyClass.property2 = dr.IsNull(2) ? String.Empty : (string)dr.ItemArray[3];
MyClass.property3 = dr.IsNull(3) ? String.Empty : (string)dr.ItemArray[3];
}
instead of
foreach (DataRow dr in ds.Tables[0].Rows)
{
MyClass.property2 = dr.IsNull(2) ? String.Empty : (string)dr.ItemArray[2];
MyClass.property3 = dr.IsNull(3) ? String.Empty : (string)dr.ItemArray[3];
}
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
|
|
|
|
|
Notes and EnglishNotes need to be properties for the binding to work properly.
public string Notes;
private string _notes;
public string Notes { get { return _notes; } set { _notes = value; } }
public string Notes { get; set; }
|
|
|
|
|
Your xaml code looks ok.
Try and have a look at the place where you are populating the model and make sure right values are being passed into the model attributes.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
|
|
|
|
|
Hi,
I have Question about the use of Observable Collection? During reading about MVVM Pattern i read about the observable collection that it reflect the changes to the UI Automatically which is not possible in the List collection.But i really dont understand it what it exactly means in practical terms. I want a demo for the Difference between the List VS ObservableCollection Vs PropertyChangedEventHandler. I have read the document on your site but i am yet not clear about it? will you please help me?
|
|
|
|
|
The ObservableCollection implements the INotifyCollectionChanged interface, so it fires a CollectionChanged event whenever an element is added, removed, replaced, or moved.
The binding system knows how to react to objects that implement INotifyCollectionChanged and INotifyPropertyChanged... So when you bind to one of those, the binding system hooks the event and updates itself when it gets notified of a change.
The List class doesn't implement that interface.
|
|
|
|