wpf 如何在 XAML 中使用带有 MouseUp 和 MouseDown 事件的按钮上的交互触发器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18990102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:43:23  来源:igfitidea点击:

How to use Interaction Trigger on button with MouseUp and MouseDown events in XAML?

wpfxaml

提问by AXG1010

I have a button that needs to execute two separate commands (one to start something and one to stop it). After doing some research the System.Windows.Interactivity.dll seemed to provide an easy way to accomplish this. But it doesn't work with the left mouse button (it does work if I use an event like MouseDoubleClickor MouseRightButtonDown, but not MouseDown, MouseUp, or MouseRightButtonDown)...it seems as if the button consumes the event itself and the interaction.trigger never sees it. I provided a snippet of my XAML below, what can I do to get around this behavior?

我有一个按钮需要执行两个单独的命令(一个启动某事,一个停止它)。在做了一些研究之后,System.Windows.Interactivity.dll 似乎提供了一种简单的方法来实现这一点。但它不适用于鼠标左键(如果我使用像MouseDoubleClickMouseRightButtonDown这样的事件,它确实有效,但不使用MouseDownMouseUpMouseRightButtonDown)......似乎按钮消耗了事件本身和交互.trigger 永远不会看到它。我在下面提供了我的 XAML 片段,我可以做些什么来解决这种行为?

<Button Content="DoStuff">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDown">
            <i:InvokeCommandAction Command="{Binding StartCommand}" />
        </i:EventTrigger>
        <i:EventTrigger EventName="MouseUp">
            <i:InvokeCommandAction Command="{Binding StopCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

回答by Omri Btian

You can use PreviewMouseDownand PreviewMouseUpinstead

您可以使用PreviewMouseDownandPreviewMouseUp代替

回答by eran otzap

mouseleftbuttondown , mouseleftbuttonup

mouseleftbuttondown , mouseleftbuttonup