3
votes

I have some doubts about what are the best practices for MVVM using parent-child model relations.

In that specific case there are two models (data classes) called Group and Contact. The group is containing a list of contacts. Both of them are are implementing INotifyPropertyChanged interface.

In the view, there is a treeview displaying the hierarchy using DataTemplate and the associated ViewModel contains an ObservableCollections property.

I am wondering what is the best practice design in this case.... Having one property like above in the ViewModel which is bind to the xaml or createing a ViewModel for each model (like GroupViewModel and ContactViewModel) and instead of ObservableCollections having an List.

What is the best way (design wise)? Shoudl I bind the Model or the ViewModel to the xaml?

1
You should bind to the view-model.Big Daddy

1 Answers

4
votes

I'm afraid, you mixed some things up. The basics of MVVM are

  • Model - Contains the data the application is working with. It should be kept as simple as possible.
  • ViewModel - Reflects the state of the application and contains the business logic. It's the business layer.
  • View - Interprets the ViewModel to provide a visual representation of the business layer and its state.

With this three parts, it's pretty easy to provide separation of concerns and a decoupled architecture. If you want to read more, click here.

Back to your questions:

In that specific case there are two models (data classes) called Group and Contact. The group is containing a list of contacts. Both of them are are implementing INotifyPropertyChanged interface.

That's a bit odd. Usually, you don't need to implement INotifyPropertyChanged in the model classes, since the VM should handle value changes from the view.

But it's imaginable to have that mechanism in the model layer too. But since you don't want to track changes on this layer and IMHO the VM should take care about, you don't need it.

[...] Having one property like above in the ViewModel which is bind to the xaml or createing a ViewModel for each model (like GroupViewModel and ContactViewModel) [...]

Yes, this is usually the approach. For each model class, which should be passed to the view layer, you would create a ViewModel.

[...] and instead of ObservableCollections having an List.

That's definitely a No. If you use a List<T>, the view would not be aware of the changes (add, remove) to collection.

What is the best way (design wise)? Shoudl I bind the Model or the ViewModel to the xaml?

Simply stick to MVVM. The view is aware of the VM, but the VM is not aware of the view. Additionally the VM is aware of the model, but the model isn't aware of it. That implies, that you should always bind the VM to the View.

Edit

The following is totally legit.

public class Address : ViewModelBase // implements INotifiedPropertyChanged a.s.o.
{
    public string Street { /* you know what comes here */ }
    public string ZipCode { /* ... */ }
    public string City { /* ... */ }

    /* more properties */
}

public class Person : ViewModelBase 
{
    public string Name { /* ... */ }

    public Address Address { /* ... */ }
}