wpf mvvm .. access view elements in viewmodel

前端 未结 2 1727
有刺的猬
有刺的猬 2021-02-04 17:58

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 ge

相关标签:
2条回答
  • 2021-02-04 18:33
    var v=(viewclassname)this.getview();
    

    then you can access the element in the view

    0 讨论(0)
  • 2021-02-04 18:40

    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.

    0 讨论(0)
提交回复
热议问题