SL4/MVVM: Handle MouseDragElementBehavior.Dragging event with void Foo() in VM

安稳与你 提交于 2019-11-30 09:24:01

问题


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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/7461930/sl4-mvvm-handle-mousedragelementbehavior-dragging-event-with-void-foo-in-vm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!