(EDITED)
This approach to hooking events for a ViewModel to process looks unlikely to be maintainable. To troubleshoot it it might be a good idea to use the Snoop utility to take the visual and logical trees apart, to make sure that the objects you're casting, all of which implement a RaiseEvent method, are in fact the objects you want.
You might also need to define your SourceName parameter as a resource on your Window.
Those are guesses, and they describe a very different approach to message passing than MVVM patterns call for. Rather than digging into your logical tree like that, which is a very brittle approach, use the documented and proven patterns found below and elsewhere: Access the DataContext
of your Window
and call the VM method directly. Or, implement an ICommand
in your ViewModel class and bind commands to it.
This first approach breaks the most pure approaches to MVVM but, it will work.
private void button1_Click(object sender, RoutedEventArgs e)
{
var VM = this.DataContext As WinVM;
if (VM != null)
{
VM.MyCustomEventWasRaised();
}
}
Another approach is to implement an ICommand
on your ViewModel class, and configure the Button
in the View to use the command:
<Button x:Name="Button1" Command="{Binding NeedsCleaning}" />
This latter will eliminate the need for any XAML code-behind code in most cases.