So I\'ve been searching around and cannot find out exactly how to do this. I\'m creating a user control using MVVM and would like to run a command on the \'Loaded\' event.
Try this:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
//Optional - first test if the DataContext is not a MyViewModel
if( !this.DataContext is MyViewModel) return;
//Optional - check the CanExecute
if( !((MyViewModel) this.DataContext).MyCommand.CanExecute(null) ) return;
//Execute the command
((MyViewModel) this.DataContext).MyCommand.Execute(null)
}