SL4/MVVM:在VM中使用void Foo()处理MouseDragElementBehavior.Dragging事件 [英] SL4/MVVM: Handle MouseDragElementBehavior.Dragging event with void Foo() in VM

查看:116
本文介绍了SL4/MVVM:在VM中使用void Foo()处理MouseDragElementBehavior.Dragging事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理我拥有的控件上的MouseDragElementBehavior.Dragging事件.请参阅此处了解为什么要执行此操作的背景.

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.

我在组织此活动时遇到了麻烦.从XAML中,您可以看到我已将行为添加到用户控件中.然后,我尝试通过CallMethodAction EventTrigger向行为的Draging事件添加处理程序.

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?

推荐答案

问题是EventTrigger没有连接到Behavior的事件.相反,它与行为的AssociatedObject事件关联.这是相关的源代码:

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:

 protected override void OnAttached()
    {
        base.OnAttached();
        DependencyObject associatedObject = base.AssociatedObject;
        Behavior behavior = associatedObject as Behavior;
        FrameworkElement element = associatedObject as FrameworkElement;
        this.RegisterSourceChanged();
        if (behavior != null)
        {
            associatedObject = ((IAttachedObject) behavior).AssociatedObject;
            behavior.AssociatedObjectChanged += new EventHandler(this.OnBehaviorHostChanged);
        }
        ....
  }

因此,您可以看到,如果触发器的关联对象是行为,则它将关联对象设置为行为的关联对象,即您的项集合. items集合没有拖动事件,因此什么也不会触发.

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.

这篇关于SL4/MVVM:在VM中使用void Foo()处理MouseDragElementBehavior.Dragging事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆