0

好的,所以我正在尝试将附加事件添加到控件。现在 Caliburn 并不天真地支持这一点,但我使用了另一篇文章中的代码,该文章似乎提供了一种解决方法。我基本上需要一种方法来添加这种代码this.AddHandler(RadDragAndDropManager.DragInfoEvent, new EventHandler<DragDropEventArgs>(OnDragInfo), true);,但 caliburn 不支持附加事件。这是我的代码:

<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 定义如下:

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; }
}

在我的 ViewModel 中,我只是有一个像这样的处理程序:

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

当我运行这个时,我得到的错误是base.OnEvent(args);关于No target found for method OnDropQuery.我正在努力弄清楚为什么这不起作用。

4

1 回答 1

0

发现问题,错误信息不是很明显。viewmodel 上的事件处理程序必须与 xaml 中目标方法的定义相匹配。在这种情况下,通过如下修改 xaml 解决了该问题:

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

我也不得不删除cal:Action.TargetWithoutContext="{Binding Source={x:Static RelativeSource.Self}}"

于 2013-01-15T18:35:26.083 回答