I'm trying to work out an issue I'm having with implementing MVVM in WPF. My Contact
class below is my model that's being populated by Entity Framework.
public class Contact : INotifyPropertyChanged
{
public string _firstName;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
OnPropertyChanged("FirstName");
}
}
public string _lastName;
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
OnPropertyChanged("LastName");
}
}
//INotifyPropertyChanged implementation omitted for brevity
}
Here's my ViewModel:
public class ContactViewModel
{
public Contact MyContact { get; set; }
public string FullName
{
get
{
return MyContact.FirstName + " " + MyContact.LastName;
}
}
}
So I set my View's datasource to an instance of ContactViewModel
, and I'm binding two TextBoxes to MyContact.FirstName
and MyContact.LastName
. I'm binding a TextBlock
to FullName
. When I change either of my TextBoxes the Full Name TextBlock
doesn't update (obviously, I'm not doing an OnPropertyChanged("FullName")
anywhere).
The question is, where do I add OnPropertyChanged("FullName")
? I don't necessarily want to modify my model because it's being used elsewhere and I don't to tie it to my ViewModel.
Do I need to rethink my architecture?