MVVM - Bind directly to Model object exposed from VM or implement a separate property in VM to access the Model properties MVVM - Bind directly to Model object exposed from VM or implement a separate property in VM to access the Model properties wpf wpf

MVVM - Bind directly to Model object exposed from VM or implement a separate property in VM to access the Model properties


If you take a look at How Data Binding References are Resolved, you can see that there can be performance issues to consider when deciding how to expose the property you are going to bind to.

Does the Model.Document implement the INotifyPropertyChanged interface? If not, I would recommend adding a Title property to your view model and implement INotifyPropertyChanged on your view model such that when the Title is changed the PropertyChanged event is raised to notify the view.

Another approach would be to expose the Title on your view model as a DependencyProperty as the binding and render time is faster.


The latter is the better way to do it, at least IMO. I haven't really seen in done the other way. The viewmodel serves as a mediator between the view and model, so those two shouldn't really have any knowledge of each other.

Here's a simple example.


It really depends on your situation. In the truest sense, the View should only interact with the View Model. But both approaches above have their advantages.

By exposing the Document property, you don't have to duplicate all the properties that you need to access on it. So if you have numerous properties, or are using some sort of reflection (such as displaying Document in a PropertyGrid), then this way is probably better.

The latter approach gives you more control over the value exposed to the View. So you can change the title (i.e. remove extra spaces) before it's committed to the model or various other actions. You can even swap out of the Document, without the View being aware of it (or even caring).