tags:

views:

348

answers:

1

I am in phase of learning wpf/mvvm as per i get to know in vm we declare commands and bind them to view element's event rather doing this in codebehind file... what i am not getting is, how would we access view elements and eventarguments.

+1  A: 

Your ViewModels won't access the elements in the View directly. The concept is that the View will bind to the ViewModel, and not the other way around. So; your ViewModel will tell the View what to display through values set in properties. If your View needs to display something it will have a databinding to the property giving this.

The commands will be held by a ViewModel, and you can bind them too directly. If you need the command to update values for the View this can be done by holding a reference from the command to the necessary ViewModel. (The ViewModel holding the Command can e.g. inject itself to the command on creation). Then the command can tell the ViewModel to update something, and this will be reflected in the View through data bindings.

For general introduction to the MVVM pattern you can check out this question which was asked a few days ago: Learning MVVM for WPF.

And hey; start accepting answers to your questions if you expect to keep getting help!

stiank81
happy! i marked
Muhammad Adnan
anyhow thanks for the info.. but would be grateful if you reply about dialogs and give some snippet to clear... thanks man
Muhammad Adnan
For basic data bindings you should find your answer here: http://stackoverflow.com/questions/1153147/one-sentence-explanation-to-mvvm-in-wpf/1153246#1153246. I'm busy at the moment, but will see if I can find time for adding some snippets on commands etc later on today.
stiank81
You can e.g. have your Commands tell the ViewModel to spawn dialogs. The response from the dialogs can be set as value in the ViewModel, and the View will get it through data bindings.
stiank81