SL4/MVVM:在 VM 中使用 void Foo() 处理 MouseDragElementBehavior.Dragging 事件
我正在尝试处理我拥有的控件上的 MouseDragElementBehavior.Dragging 事件。请参阅这里了解我为什么要这样做的背景。
我在安排此活动时遇到问题。从 XAML 中您可以看到我已向用户控件添加了一个行为。然后,我尝试通过 CallMethodAction EventTrigger 将处理程序添加到行为上的 Dragging 事件。
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior ConstrainToParentBounds="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Dragging">
<ei:CallMethodAction MethodName="NotifyChildrenYouAreDragging" TargetObject="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ei:MouseDragElementBehavior>
</i:Interaction.Behaviors>
我尝试了以下方法签名,但没有成功:
void NotifyChildrenYouAreDragging(){}
void NotifyChildrenYouAreDragging(object sender, EventArgs e){}
void NotifyChildrenYouAreDragging(object sender, MouseEventArgs e){}
任何人都有使用触发器来处理附加行为中的事件的经验吗?
I am trying to handle the MouseDragElementBehavior.Dragging event on a control I have. See here for background on why I want to do this.
I am having trouble wiring up this event. From the XAML you can see I have added a behavior to the user control. Then I attempted to add a handler to the Dragging event on the behavior via the CallMethodAction EventTrigger.
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior ConstrainToParentBounds="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Dragging">
<ei:CallMethodAction MethodName="NotifyChildrenYouAreDragging" TargetObject="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ei:MouseDragElementBehavior>
</i:Interaction.Behaviors>
I have tried the following method signatures with no luck:
void NotifyChildrenYouAreDragging(){}
void NotifyChildrenYouAreDragging(object sender, EventArgs e){}
void NotifyChildrenYouAreDragging(object sender, MouseEventArgs e){}
Anyone have experience using triggers to handle events in attached behaviors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 EventTrigger 没有连接到 Behavior 的事件。相反,它连接到Behavior 的AssociatedObject 事件。下面是相关的源代码:
所以你可以看到,如果触发器的关联对象是一个行为,那么它会将关联对象设置为行为的关联对象,即你的项目集合。 items 集合没有拖动事件,因此不会触发任何事件。
您可以通过创建另一个行为来检查关联对象是否具有拖动行为,如果有,则将您的行为附加到拖动事件,从而获得所需的结果。然后从那里调用对象的方法。
The problem is that the EventTrigger doesn't hook up to the Behavior's events. Instead it is hooking up to the Behavior's AssociatedObject's events. Here is the relevant source code:
So you can see that if the associated object of the trigger is a behavior, then it sets the associated object to the Behavior's associated object which is your items collection. The items collection doesn't have a dragging event so nothing is ever fired.
You could probably get the results you want by creating another behavior that checks to see if the associated object has a drag behavior and if so have your behavior attach to the dragging event. Then call the method on the object from there.