WPF - Declaring a custom routed event and listening to it
The reason is in how you raise that event:
private void Button_Click(object sender, RoutedEventArgs e)
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(FuffaControl.FuffaEvent);
RaiseEvent(newEventArgs);
}
Routed event (like regular .NET event) has source (sender) and arguments. You only specify arguments, and sender is the control on which you call RaiseEvent
. You do this from MainWindow
class, so source of event will be MainWindow, not button (button does not participate in your event raising code at all as you may notice). WPF will search handlers for routing event starting from sender and then will go up or down hierarchy, depending on event type. In your case event is bubbling so it will search up the tree, starting from MainWindow. Your control is child of window so its handler will not be found.
Instead you should call RaiseEvent
on button. Then button will be sender and it will work as you expect:
private void Button_Click(object sender, RoutedEventArgs e) {
((FrameworkElement) sender).RaiseEvent(new RoutedEventArgs(FuffaControl.FuffaEvent));
}