I’m using Caliburn.Micro and I have the MainWindowViewModel setup with a child VM in a content control. In the Main VM I have a Child VM property which allows me to share properties across the VMs. I have a UserName property on both VMs.
On the Main View I am adding a string to the end of the username to personalize it, while on the child View I’m just displaying the Username as is. The username property is only editable in the child VM, but not the main VM. When I edit it in the child it does not update the parent. I’m not sure how to bind these properties to get the Parent VM in sync with the Child VM.
Main VM:
public string UserName
{
get { return string.Format("{0}'s Diary Log", DiaryViewModel.UserName); }
}
private DiaryViewModel _diaryViewModel;
public DiaryViewModel DiaryViewModel
{
get { return _diaryViewModel; }
set
{
_diaryViewModel = value;
NotifyOfPropertyChange(() => DiaryViewModel);
}
}
Child VM:
private string _userName = "John";
public string UserName
{
get { return _userName; }
set
{
_userName = value;
NotifyOfPropertyChange(() => UserName);
}
}
You could just bind your control in the MainView to DiaryViewModel.UserName and do the string formatting right in the .xaml. Don’t forget to escape the apostrophe in the string formatting with a slash.