I have a custom behavior inside a DataTemplate, which raises a custom Routed Event. I want to handle that event with a TriggerAction (send a message, invoke a command, etc).
<Border Background="#01FFFFFF" VerticalAlignment="Center">
<i:Interaction.Behaviors>
<Behaviors:MyBehavior>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MyEvent">
<triggers:SendMessageAction Parameter="ActionTaskAssign" MessageToken="ActionTask" />
</i:CustomEventTrigger>
</i:Interaction.Triggers>
</Behaviors:DropBehavior>
</i:Interaction.Behaviors>
<!-- .... -->
Yet I can clearly tell, the EventTrigger is NOT subscribing to the behavior’s MyEvent.
- I’ve tried setting the SourceObject from binding (seen here) but ElementName binding doesn’t seem to work, and neither does FindAncestor
- I’ve tried inheriting from EventTrigger and setting the SourceObject in code, all I got was a StackOverflowException
- I’ve tried writing a custom EventTrigger, but I either write one for every custom event (if all hell breaks loose, I will), or I have to figure out a generic way to handle non-generic
RoutedEventHandlers…
What should I do?
The problem is that the EventTrigger doesn’t hook up to the Behavior’s events. Instead it is hooking up to the Behavior’s AssociatedObject’s events. Here is the relevant source code:
}
A related question was asked here :
Handle MouseDragElementBehavior.Dragging event with void Foo() in VM
I solved this myself with writing my own custom behavior that fires off a command (which I needed rather then your message.
You can see my approach in the answer here :
Custom behavior with command