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

前端 未结 1 1542
臣服心动
臣服心动 2020-12-21 07:12

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

相关标签:
1条回答
  • 2020-12-21 08:01

    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.

    0 讨论(0)
提交回复
热议问题