[英]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.