MVVM access model from view

I am new to .net and MVVM designmodel. I am stuck now because in the codebehind file of one of my views I need to get the corresponding model as an object. How is the relationship here? Is it possible, and this is the "right way to do it"?

+5
source share
4 answers

The view must have access to the ViewModel, which wraps the model, hiding it from the view. If you need to access Model properties from a view, get a ViewModel to expose them as properties that can bind to a view.

+5
source

ViewModel . View DataBinding , ViewModel.

, DataBinding. .

, ViewModel .

+1

MVVM View . MVVM.

ViewModel , .

:

{Binding Model.Title}

:

((MyViewModel)DataContext).Title

The WPF Application Framework (WAF) ViewModel (EmailClient) sample application shows how to access a model from a view.

+1
source
public partial class MyView : Window
{
     private MyViewModel aModel;

     public MyView()
     {
         InitializeComponent();
         aModel = new MyViewModel();
         this.DataContext = aModel();
}

The view will now change its control data bindings associated with the public properties of the ViewModel.

0
source

All Articles