如何在< button/>上使用mouseDown和mouseUp [英] How to use mouseDown and mouseUp on <button/>

查看:146
本文介绍了如何在< button/>上使用mouseDown和mouseUp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XAML中声明了一个按钮,该按钮具有同时调用指定方法的MouseDown和MouseUp属性.

I have a button declared in XAML which has MouseDown and MouseUp attributes that both call a specified method...

<Button x:Name="btnBackward" Content="Backward"
        MouseDown="btnBackward_MouseDown" 
        MouseUp="btnBackward_MouseReleased" Width="50" Height="25" 
        Margin="65,400,377,45"/>

但是,永远不会调用btnBackward_MouseReleased方法.

However, the method btnBackward_MouseReleased is never called.

private void btnBackward_MouseReleased(object sender, 
                                       System.Windows.Input.MouseEventArgs e)
{
    Console.WriteLine("mousereleased");
    this.isRewinding = false;
}

我想念什么?

推荐答案

您应在此处使用Preview events.因此,钩住PreviewMouseDownPreviewMouseUp而不是MouseDownMouseUp.

You should use Preview events here. So, instead of MouseDown and MouseUp, hook to PreviewMouseDown and PreviewMouseUp.

<Button x:Name="btnBackward" Content="Backward"
        PreviewMouseDown="btnBackward_MouseDown" 
        PreviewMouseUp="btnBackward_MouseReleased"/>

原因表 MSDN -

Button抑制MouseLeftButtonDown和MouseLeftButtonDown Button中的Button或其组合元素引发的冒泡事件 倾向于捕获鼠标并引发始终发生的Click事件 由Button本身引发.事件及其数据仍在继续 沿路线,但是因为按钮将事件数据标记为 已处理,仅事件处理者明确指出他们 应该在被调用的handledEventsToo情况下采取行动.如果其他元素 朝向您的应用程序的根仍然希望有机会 处理受控制的事件,一种替代方法是附加 代码中将handleEventsToo指定为true的处理程序.但是通常 更简单的方法是将您要处理的路由方向更改为 相当于输入事件的预览. 例如,如果一个控件 抑制MouseLeftButtonDown,请尝试为 而是使用PreviewMouseLeftButtonDown .

Button suppresses MouseLeftButtonDown and MouseLeftButtonDown bubbling events raised by the Button or its composite elements in favor of capturing the mouse and raising a Click event that is always raised by the Button itself. The event and its data still continue along the route, but because the Button marks the event data as Handled, only handlers for the event that specifically indicated they should act in the handledEventsToo case are invoked. If other elements towards the root of your application still wanted an opportunity to handle a control-suppressed event, one alternative is to attach handlers in code with handledEventsToo specified as true. But often a simpler technique is to change the routing direction you handle to be the Preview equivalent of an input event. For instance, if a control suppresses MouseLeftButtonDown, try attaching a handler for PreviewMouseLeftButtonDown instead.

但是,如果您右键单击按钮MouseUpMouseDown事件将非常有效,因为在这种情况下click不会占用事件,因此它们会正确冒泡.

However, if you right click on your button MouseUp and MouseDown events will work perfectly since click doesn't eat up the event in that case and they are properly bubbled up.

这篇关于如何在&lt; button/&gt;上使用mouseDown和mouseUp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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