Frage

Ok, so I'm trying to add attached event to a control. Now Caliburn doesn't support this naively, but I used code from a different article that seems to provide a workaround. I basically need a way to add this kind of code this.AddHandler(RadDragAndDropManager.DragInfoEvent, new EventHandler<DragDropEventArgs>(OnDragInfo), true);, but caliburn doesn't support attached events. Here's my code:

<telerik:RadTreeView Height="250" Name="Root" Width="300" ItemTemplate="{StaticResource BusinessEntityTemplate}" IsDragDropEnabled="true"  cal:Action.TargetWithoutContext="{Binding Source={x:Static RelativeSource.Self}}">
    <i:Interaction.Triggers>
    <Helpers:RoutedEventTrigger RoutedEvent="{x:Static dragDrop:RadDragAndDropManager.DropQueryEvent}" IncludeHandledEvents="True">
        <cal:ActionMessage MethodName="OnDropQuery">
        <!--<cal:Parameter Value="$eventargs" />-->
        </cal:ActionMessage>
    </Helpers:RoutedEventTrigger>
    </i:Interaction.Triggers>
    </telerik:RadTreeViewItem>-->
</telerik:RadTreeView>

RoutedEventTrigger is defined as following:

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

    public bool IncludeHandledEvents
    {
        get { return _includeHandledEvents; }
        set { _includeHandledEvents = 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)); }
        { associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent), this.IncludeHandledEvents); }

    }
    void OnRoutedEvent(object sender, RoutedEventArgs args)
    {
        base.OnEvent(args);
    }

    protected override string GetEventName() { return RoutedEvent.Name; }
}

and in my ViewModel I simply have a handler like this:

public void OnDropQuery(object sender, DragDropQueryEventArgs e) {}

When I run this the error that I get is on base.OnEvent(args); about No target found for method OnDropQuery. I'm struggling to figure out why this isn't working.

War es hilfreich?

Lösung

Found the problem, error message isn't very obvious about this. The event handler on viewmodel has to match the definition of the target method in the xaml. In this case, the issue was fixed by modifying the xaml as following:

                <cal:ActionMessage MethodName="OnDropQuery">
                    <cal:Parameter Value="$source" />
                    <cal:Parameter Value="$eventargs" />
                </cal:ActionMessage>

I also had to remove cal:Action.TargetWithoutContext="{Binding Source={x:Static RelativeSource.Self}}"

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top