The following code illustrates my problem:
public class Person : (INotifyPropertyChanged) // INPC only for PersonViewModelB
{
public string Name {get; set;}
}
public class PersonViewModelA : INotifyPropertyChanged
{
private Person _person {get; set;}
public string Name
{
get { return _person.Name; }
set { _person.Name = value; }
}
//XAML: {Binding Name}
}
public class PersonViewModelB : INotifyPropertyChanged
{
private Person Person {get; set;}
//XAML: {Binding Person.Name}
}
In many examples I have seen something like PersonViewModelA. I don’t understand the benefit. For me PersonViewModelB is much more clean and much more DRY.
In both cases I can add additional view specific fields to the ViewModel (e.g. IsSelected) or calculated fields (e.g. Name = FirstName + ” ” + LastName). If I have a richt domain model, why I can’t expose it to the view?
In Asp.net MVC we have learned to bind directy to the model and there it is just fine, but in WPF it is not? Why?
What justifies the additional overhead?
MVVM is different from MVC which has no View-Model (and is more of a triangle than a line). One reason to not bind directly to the model is to keep the dependencies minimal, so that if the model changes you may possibly only need to adjust the VM not the VM and the View.