我正在学习 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),相应的按钮才会经历角度转换。你能帮我解决这个问题吗?
更新:
如果我用椭圆替换按钮,这个片段似乎有效。当椭圆可以时,为什么按钮不能对左键做出反应。如果我多次单击同一个椭圆,也不会引发事件