SL4/MVVM:在 VM 中使用 void Foo() 处理 MouseDragElementBehavior.Dragging 事件

发布于 2024-12-05 08:00:41 字数 1082 浏览 1 评论 0原文

我正在尝试处理我拥有的控件上的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生百味 2024-12-12 08:00:41

问题是 EventTrigger 没有连接到 Behavior 的事件。相反,它连接到Behavior 的AssociatedObject 事件。下面是相关的源代码:

 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 集合没有拖动事件,因此不会触发任何事件。

您可以通过创建另一个行为来检查关联对象是否具有拖动行为,如果有,则将您的行为附加到拖动事件,从而获得所需的结果。然后从那里调用对象的方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文