简体   繁体   English

如何检查 wpf C# 中鼠标按钮是左键还是右键?

[英]How can I check if mouse button is left or right in wpf C#?

I'm trying this code actually I have created only one Eventhandler that is on Click="button_Click" .我正在尝试这段代码,实际上我只创建了一个位于Click="button_Click"上的 Eventhandler。

XAML: XAML:

<Window x:Class="WPFAPP.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPFAPP"
    mc:Ignorable="d"
    Title="Window1" Height="447.625" Width="562">

    <Grid>
        <Button x:Name="btn1" Content="Button1" Click="button_Click" HorizontalAlignment="Left" Margin="26,22,0,0" VerticalAlignment="Top" Width="75"/>
        <Button x:Name="btn2" Content="Button2" Click="button_Click" HorizontalAlignment="Left" Margin="26,61,0,0" VerticalAlignment="Top" Width="75"/>
        <Button x:Name="btn3" Content="Button3" Click="button_Click" HorizontalAlignment="Left" Margin="26,100,0,0" VerticalAlignment="Top" Width="75"/>
        <Button x:Name="btn4" Content="Button4" Click="button_Click" HorizontalAlignment="Left" Margin="26,137,0,0" VerticalAlignment="Top" Width="75"/>
        <Button x:Name="btn5" Content="Button5" Click="button_Click" HorizontalAlignment="Left" Margin="26,174,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window> 

Code Behind C#: C# 背后的代码:

private void button_Click(object sender, RoutedEventArgs e)
{
    Button button = (Button)sender;
    if(e.Equals(Mouse.RightButton))
    {
        button.ClearValue(Button.BackgroundProperty);
        button.Background = Brushes.Green;
    } 
} 

Click is only designed for the most limited interaction, if you use the more advanced mouse events you then get a MouseButtonEventArgs which gives you all details about the event. Click仅适用于最有限的交互,如果您使用更高级的鼠标事件,则会获得MouseButtonEventArgs,它会为您提供有关该事件的所有详细信息。

the reason for this is that Click isn't a mouse event, you could also trigger it with a touch, stylus, you can even trigger it with the keyboard by pressing Return while highlighted 这样做的原因是Click不是鼠标事件,您也可以通过触控,触控笔触发它,您甚至可以通过按下Return键来触发它,同时突出显示

so try MouseDown, MouseUp or DoubleClick instead 所以请尝试使用MouseDown,MouseUp或DoubleClick

eg 例如

<Button MouseDoubleClick="Button_MouseDoubleClick" >Click me</Button>
private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if(e.ChangedButton == MouseButton.Right)
    {
    }
    e.Handled = true;
}

or for mouse down 或者用于鼠标按下

<Button MouseDown="Button_MouseDown" >Click me</Button>
private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
    if(e.ChangedButton == MouseButton.Right)
    {
    }
    e.Handled = true;
}

It's always better to have separate event handler for left and right mouse down. 左右鼠标分别设置单独的事件处理程序总是更好。 If you need to work as per the clicked button. 如果您需要按照单击的按钮工作。 Below is my approach. 以下是我的方法。

<Button MouseLeftButtonDown="Button_MouseLeftButtonDown" MouseRightButtonDown="Button_MouseRightButtonDown"></Button>

Below is code behind. 下面是代码背后。

private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{

}

private void Button_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{

}

If you want to get the button which cause the event, you can check the e.OriginalSource property. 如果要获取导致事件的按钮,可以检查e.OriginalSource属性。

By using MVVM you can do for Left button Double Click on a ListView Item for example:通过使用 MVVM,您可以为左键双击一个 ListView 项目执行操作,例如:

The Namespace to add in "Window" or "UserControl"在“Window”或“UserControl”中添加的命名空间

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

The ListView:列表视图:

<ListView x:Name="lvComputers"
          IsSynchronizedWithCurrentItem="True"
          ...>
  <!-- Double Click -->
  <i:Interaction.Triggers>
     <i:EventTrigger EventName="MouseDoubleClick">
        <i:InvokeCommandAction Command="{Binding ListViewComputers_MouseDoubleClick}"
                               CommandParameter="{Binding ElementName=lvComputers, Path=SelectedItem}"
                               />
     </i:EventTrigger>
  </i:Interaction.Triggers>
</ListView>

and then the binded event:然后是绑定事件:

private void ListViewComputers_MouseDoubleClick(object obj)
{
   //Allow only left button double click
   if (Mouse.LeftButton == MouseButtonState.Pressed)
   {
      //Code to execute on Left Button Double Click
   }
}

"Mouse" needs: “鼠标”需要:

using System.Windows.Input;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在c#中右键或左键单击检查按钮 - How i can Check button click is right or left in c# 如何更改以下 Unity C# 游戏对象左/右滑动代码以进行鼠标单击? - How can I change the following Unity C# gameobject left/right swipe code for Mouse clicks? 如何检查 c# 中的鼠标左键是否按下 ctrl、alt? - How can i check if ctrl,alt are pressed on left mouse click in c#? 在c#中如何检查鼠标是否移动,而鼠标左键是否按下 - How to check if the mouse moves, while the left mouse button is down, in c# 如何在 C# WinForms 按钮中左右对齐文本? - How can I align text both left and right in a C# WinForms button? 如何在C#WPF中的画布上侦听鼠标左键? - How can I listen for left mouseclicks on a canvas in a C# WPF? 如何使鼠标右键单击自动拍摄,如果再次单击鼠标左键,则每次单击鼠标都会拍摄一次? - How can I make right mouse button click to shoot automatic and if clicking again the mouse left button it will shoot single each left mouse click? 在WPF / C#中,如何检查中心按钮是否被单击或释放? - In WPF / C#, how can I check if the center button is clicked or released? 如何在WPF中释放鼠标左键时触发事件? - How can I trigger an event when the left mouse button gets released in WPF? WPF C#XAML ListBox鼠标事件(右键单击与左键单击) - WPF C# XAML ListBox mouse events (right click vs left click)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM