好的,所以我试图将附加事件添加到控件中。现在Caliburn并不天真地支持这个,但我used code from a different article似乎提供了一种解决方法。我基本上需要一种方法来添加这种代码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.
我正在努力弄清楚为什么这不起作用。
答案 0 :(得分: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}}"