LeftMouseButtonDown 不适用于路由事件中的按钮
我正在学习 wpf 中的路由事件,我尝试了以下示例:
文件 -- Window1.xaml
<ScrollViewer VerticalScrollBarVisibility="Auto">
<UniformGrid MouseDown="UniformGrid_MouseDown">
<Button x:Name="Button1">1</Button>
<Button x:Name="Button2">2</Button>
<Button x:Name="Button3">3</Button>
<Button x:Name="Button4">4</Button>
<Button x:Name="Button5">5</Button>
<Button x:Name="Button6">6</Button>
<Button x:Name="Button7">7</Button>
<Button x:Name="Button8">8</Button>
<Button x:Name="Button9">9</Button>
</UniformGrid>
</ScrollViewer>
文件 -- Window1.xaml.cs
private void UniformGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
Button aTargetButton = e.Source as Button;
if (aTargetButton != null)
{
aTargetButton.Background = Brushes.Azure;
aTargetButton.LayoutTransform = new RotateTransform(45);
if (myPreviouslyClickedButton != null)
{
myPreviouslyClickedButton.Background = Brushes.White;
myPreviouslyClickedButton.LayoutTransform = new RotateTransform(0);
}
myPreviouslyClickedButton = aTargetButton;
}
}
当我运行这些片段时,仅当我右键单击时,相应的按钮才会经历角度转换它(即使我已经订阅了 MouseDown)。 你能帮我解决这个问题吗?
更新:
如果我用椭圆形替换按钮,这个片段似乎可以工作。 为什么按钮不能对左键单击做出反应,而椭圆可以。 此外,如果我多次单击同一个椭圆,则不会引发事件
I was learning about routed events in wpf and I tries the following example,
File -- Window1.xaml
<ScrollViewer VerticalScrollBarVisibility="Auto">
<UniformGrid MouseDown="UniformGrid_MouseDown">
<Button x:Name="Button1">1</Button>
<Button x:Name="Button2">2</Button>
<Button x:Name="Button3">3</Button>
<Button x:Name="Button4">4</Button>
<Button x:Name="Button5">5</Button>
<Button x:Name="Button6">6</Button>
<Button x:Name="Button7">7</Button>
<Button x:Name="Button8">8</Button>
<Button x:Name="Button9">9</Button>
</UniformGrid>
</ScrollViewer>
File -- Window1.xaml.cs
private void UniformGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
Button aTargetButton = e.Source as Button;
if (aTargetButton != null)
{
aTargetButton.Background = Brushes.Azure;
aTargetButton.LayoutTransform = new RotateTransform(45);
if (myPreviouslyClickedButton != null)
{
myPreviouslyClickedButton.Background = Brushes.White;
myPreviouslyClickedButton.LayoutTransform = new RotateTransform(0);
}
myPreviouslyClickedButton = aTargetButton;
}
}
When I ran these snippets, the corresponding button undergoes a Angular Transformation only when I do a right click over it (even though I have subscribed for MouseDown). Can you help me out with this?
Update:
This snippet seems to work if i replace the button with an ellipse. Why can't the button react to left clicks when an ellipse can. Also the events are not raised if i click on the same ellipse more then once
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的鼠标按下事件由按钮处理。
如果您希望网格处理按钮事件,那么您可以执行以下操作:
并将源更改为类似以下内容:
有一篇 msdn 文章 这里值得一读。
Your mouse down event is being handled by the button.
If you want your grid to handle the button events, then you might do something like:
and change the source to something like:
There's an msdn article here that would be worth a read.
这是正常工作的。 在内部,
Button
处理MouseDown
事件以了解何时触发其Click
事件。 我发现 Snoop 是一个很好的工具,用于跟踪 WPF 事件问题,或者只是获取更好地了解它们的工作原理。This is working correctly. Internally, the
Button
handles theMouseDown
event to know when to fire off itsClick
event. I've found that Snoop is a great tool for tracking down problems with WPF events, or to just get a better understanding of how they work.