在 WPF 中释放鼠标左键时如何触发事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25435134/
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 12:23:49  来源:igfitidea点击:

How can I trigger an event when the left mouse button gets released in WPF?

c#wpfxaml

提问by KrisW

I need to trigger an event when the left mouse button gets released. I've tried this:

我需要在释放鼠标左键时触发一个事件。我试过这个:

<i:Interaction.Triggers>
   <i:EventTrigger EventName="MouseClick" >
      <i:InvokeCommandAction Command="{Binding OnBarGroupChangeCommand}"    CommandParameter="{Binding ElementName=ReportsBarGroup, Path=Key}"  />
   </i:EventTrigger>
</i:Interaction.Triggers>

and this

和这个

        <igWPF:OutlookBarGroup.InputBindings>
            <MouseBinding MouseAction="LeftClick" 
                          Command="{Binding OnBarGroupChangeCommand}" CommandParameter="{Binding ElementName=ReportsBarGroup, Path=Key}"/>
        </igWPF:OutlookBarGroup.InputBindings>

These both work. The problem with both cases is that the event fires when the button gets pressed. I need it to fire only when the button gets released. The MouseBinding does not seem to support this. Is there a way to do this with Interaction? What is the best way to handle this? Thanks.

这些都有效。这两种情况的问题在于按下按钮时会触发事件。我需要它只在按钮被释放时触发。MouseBinding 似乎不支持这一点。有没有办法通过交互来做到这一点?处理这个问题的最佳方法是什么?谢谢。

回答by allen1

Try EventTrigger event name "MouseLeftButtonUp".

尝试 EventTrigger 事件名称“ MouseLeftButtonUp”。

回答by Chris

I'm not too familiar with C# but, as far as I'm aware, MouseBinding doesn't allow the support of mouse-up actions, only mouse-down. Take a look at the answer over here

我对 C# 不太熟悉,但据我所知,MouseBinding 不允许支持鼠标向上操作,只支持鼠标向下操作。看看这里的答案

回答by Fede

Why don't your try this:

为什么不试试这个:

private void btn_MouseUp(object sender, MouseEventArgs e)
    {
        /////WHAT YOU WANT THE BUTTON TO DO/////
    }

If you need to know more about mouse events enter HERE

如果您需要了解有关鼠标事件的更多信息,请输入此处