using attached events with caliburn micro Message.Attach

finally I understand the problem and the solution.
The problem is that system.windows.interactiviy.EventTrigger doesn’t support attached events.
Caliburn micro uses it for actions thus my attached event didn’t work.
The solution was to write a customized event trigger and use caliburn micro action in a explicit manner.
the customized event trigger was taken from that post:http://joyfulwpf.blogspot.com/2009/05/mvvm-invoking-command-on-attached-event.html?showComment=1323674885597#c8041424175408473805

 public class RoutedEventTrigger : EventTriggerBase<DependencyObject>
{
    RoutedEvent _routedEvent; 
    public RoutedEvent RoutedEvent
    {
        get { return _routedEvent; } 
        set { _routedEvent = value; }
    } 

    public RoutedEventTrigger() { } 
    protected override void OnAttached()
    {
        Behavior behavior = base.AssociatedObject as Behavior; 
        FrameworkElement associatedElement = base.AssociatedObject as FrameworkElement; 
        if (behavior != null)
        {
            associatedElement = ((IAttachedObject)behavior).AssociatedObject as FrameworkElement;
        } 
        if (associatedElement == null)
        {
            throw new ArgumentException("Routed Event trigger can only be associated to framework elements");
        } 
        if (RoutedEvent != null) 
        { associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent)); }
    } 
    void OnRoutedEvent(object sender, RoutedEventArgs args)
    {
        base.OnEvent(args);
    } 
    protected override string GetEventName() { return RoutedEvent.Name; }
}

and then when you want to use caliburn action:

<i:Interaction.Triggers>
                <!--in the routed event property you need to put the full name space and event name-->
                <Helpers:RoutedEventTrigger RoutedEvent="Helpers:DataChanging.Changing">
                    <cal:ActionMessage MethodName="SelectedDataChanged">
                        <cal:Parameter Value="$eventargs" />
                    </cal:ActionMessage>
                </Helpers:RoutedEventTrigger>
 </i:Interaction.Triggers>

Leave a Comment