Loaded="UserControl_Loaded"
<UserControl.Resources>
<viewModels:PageViewModel x:Key="vm" />
</UserControl.Resources>
In the Loaded handler, page.xaml.cs:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
PageViewModel pvm = this.DataContext as PageViewModel;
System.Diagnostics.Debug.WriteLine("pvm is " + ((null == pvm) ? "null" : pvm.ToString()));
}
Am I doing something wrong, or is the VM not available, yet, when the View fires the Loaded event?
UPDATE: The real issue appears to be the VM is not getting hooked up to the View. In UserControl_Loaded, pvm is null.
Currently, I am setting the DataContext on the root element of the layout; does it need to be set on the UserControl? If so, how?Thanks for any insight...
Replace
<UserControl.Resources>
<viewModels:PageViewModel x:Key="vm" />
</UserControl.Resources>
by :
<UserControl.DataContext>
<viewModels:PageViewModel />
</UserControl.DataContext>
精彩评论