I need to figure out how to communicate between ViewModels. I'm new to MVVM so please be kind.
Here's a dumbed down example
class definitions(assume that I have hooked the Child.PropertyChanged event in the ParentViewModel):
public class ParentViewModel : ViewModelBase
{
public ChildViewModel Child { get; set; }
}
public class ChildViewModel : ViewModelBase
{
String _FirstName;
public String FirstName
{
get { return _FirstName; }
set
{
_FirstName = value;
OnPropertyChanged("FirstName");
}
}
}
Here's what you see in the resource dictionary
<DataTemplate DataType="{x:Type vm:ParentViewModel}">
<vw:ParentView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ChildViewModel}">
<vw:ChildView/>
</DataTemplate>
and the code-behind of the ChildView:
public partial class QueueView : UserControl
{
public QueueView()
{
InitializeComponent();
DataContext = new QueueViewModel();
}
}
The obvious problem is that when the ChildView gets instantiated (via selection from the DataTemplate) it creates a new ChildViewModel class and the ParentViewModel doesn't have access to it.
So how can I instantiate the DataContext of the View to be the original ViewModel that caused the DataTemplate to be selected?
An obvious fix is to mmerge the properties in the ChildViewModel into the ParentViewModel, but I would rather separate it because for reuse.
I'm sure the answer is trivial, I just would like to know what it is. :)
Thanks in advance.