I have a routed event declared as such (names have been changed to protect the innocent):
public class DragHelper : DependencyObject {
public static readonly RoutedEvent DragCompleteEvent = EventManager.RegisterRoutedEvent(
"DragComplete",
RoutingStrategy.Bubble,
typeof(DragRoutedEventHandler),
typeof(DragHelper)
);
public static void AddDragCompleteHandler( DependencyObject dependencyObject, DragRoutedEventHandler handler ) {
UIElement element = dependencyObject as UIElement;
if (element != null) {
element.AddHandler(DragCompleteEvent, handler);
}
}
public static void RemoveDragCompleteHandler( DependencyObject dependencyObject, DragRoutedEventHandler handler ) {
UIElement element = dependencyObject as UIElement;
if (element != null) {
element.RemoveHandler(DragCompleteEvent, handler);
}
}
Pretty standard stuff. In XAML, I have a DataTemplate that contains a single custom control. I am attempting to attach this event (as well as some other attached properties) to the control:
<DataTemplate ...>
<My:CustomControl
My:DragHelper.IsDragSource="True"
My:DragHelper.DragComplete="DragCompleteHandler" />
</DataTemplate>
This fails to produce the desired results. Specifically, while the code that calls RaiseEvent() for the DragComplete event is called, the handler is never invoked. Nor, in fact, are the handlers for any other custom routed events that are hooked up elsewhere in this XAML file.
I tried changing the name of the routed event, and I tried switching the data template from one with a DataType to one with an x:Key. This produced no visible change in the behavior.
However, if I change My:CustomControl to any built-in WPF control, such as a TextBlock, the events are fired exactly as I would exect. Similarly, if I replace my custom control with any other custom UserControl subclass from my project, the behavior reverts to the broken, no-events-ever-seem-to-get-handled state.
This isn't make a whole lot of sense to me. Is there something specific I need to do to get this scenario to work? It seems like it should not matter. I suppose it's possible there is a specific thing I've done in all my custom controls that causes the event handling to break, but I haven't seen anything common in the three or four custom controls I've tried so far.