我有这样的代码(仅剪切部分需要;此代码用作UserControl):
<Grid>
<Image x:Name="im" HorizontalAlignment="Left" Height="120" Width="120">
<Image.RenderTransform>
<RotateTransform Angle="0" CenterX="60" CenterY="60" />
</Image.RenderTransform>
</Image>
<Button x:Name="br" Content="Right" Width="55">
<Button.Triggers>
<EventTrigger RoutedEvent="MouseUp">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="im" Storyboard.TargetProperty="RenderTransform.Angle" By="90" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
我想通过点击“br”按钮将图像(“im”)旋转90度。此代码不起作用 但是如果我使用这段代码:
<Grid>
<Image x:Name="im" HorizontalAlignment="Left" Height="120" Width="120">
<Image.RenderTransform>
<RotateTransform Angle="0" CenterX="60" CenterY="60" />
</Image.RenderTransform>
<Image.Triggers>
<EventTrigger RoutedEvent="MouseUp">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="im" Storyboard.TargetProperty="RenderTransform.Angle" By="90" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>
<Button x:Name="br" Content="Right" Width="55">
</Button>
</Grid>
旋转工作(图像通过点击旋转)。第一个出了什么问题?
答案 0 :(得分:1)
您是否尝试过Button.Click Routed事件而不是MouseUp。 MouseUp无法使用按钮的原因在此解释:Wpf event not bubbling。
您提供的代码适用于鼠标右键单击。在鼠标左键单击时,Click事件吞噬了鼠标事件。
我尝试了下面的代码,它似乎工作。我使用了虚拟图像作为源。
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Image x:Name="im" HorizontalAlignment="Left" Height="120" Width="120" Source="..\Images\info.png">
<Image.RenderTransform>
<RotateTransform Angle="0" CenterX="60" CenterY="60" />
</Image.RenderTransform>
</Image>
<Button x:Name="br" Grid.Row="1" Content="Right" Width="55">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="im" Storyboard.TargetProperty="RenderTransform.Angle" By="90" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
如果您需要更多信息,请与我们联系。