[英]MouseUp event doesnt work on left click
I am making a GUI using Windows Presentation Foundation (WPF). 我使用Windows Presentation Foundation(WPF)制作GUI。 When I mouse click a button (left or right), I want a message box shown.
当我鼠标单击一个按钮(左或右)时,我想要一个消息框显示。 So far I have managed to make an example from tutorials, but it only works when I right-click , and not when I left-click the button.
到目前为止,我已经设法从教程中做了一个例子,但它只在我右键单击时才有效,而不是在我左键单击按钮时。 I cannot see anything in my code, which should prevent left-click from working, so I hope you can help me.
我在代码中看不到任何内容,这会阻止左键单击工作,所以我希望你能帮助我。
XAML code XAML代码
<Grid>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="72">
Hello, WPF!
</TextBlock>
<!-- This button shuld activate the even MyButton_MouseUp -->
<Button Margin="200,250,200,20" Name="MyButton" MouseUp="MyButton_MouseUp">
Test
</Button>
</Grid>
C# code C#代码
// This only works on right-click
private void MyButton_MouseUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Hello world!");
}
You can subscribe to the PreviewMouseUp
's Tunneled event instead of MouseUp
: 您可以订阅
PreviewMouseUp
的Tunneled事件而不是MouseUp
:
<Button Margin="200,250,200,20" Name="MyButton" PreviewMouseUp="MyButton_MouseUp" />
The PreviewMouseUp
's Routing strategy is Tunneling , ie it will go down of the VisualTree
hierarchy, and so the Tunnel events are triggered before the Bubble events . PreviewMouseUp
的路由策略是隧道 ,即它将沿着VisualTree
层次结构,因此在Bubble事件之前触发隧道 事件 。
In addition to S. Akbari's post, this one is worth reading in order to understand why right-click works, and left-click does not... 除了S. Akbari的帖子之外,这篇文章值得一读,以便理解为什么右键单击有效, 左键单击不...
How to use mouseDown and mouseUp on <button/> 如何在<button />上使用mouseDown和mouseUp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.