Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf mvvm .. access view elements in viewmodel

Tags:

mvvm

wpf

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.

like image 236
Muhammad Adnan Avatar asked Feb 17 '10 07:02

Muhammad Adnan


1 Answers

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.

like image 125
stiank81 Avatar answered Oct 19 '22 21:10

stiank81