好的,所以我正在尝试将附加事件添加到控件。现在 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.
我正在努力弄清楚为什么这不起作用。