我正在尝试移动以及在图像的鼠标移动/源更改上缩放图像。我为Image写了以下样式,但它不起作用:
<Style x:Key="MovingImage" TargetType="{x:Type Image}">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Stretch" Value="Uniform"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Width" Value="auto"/>
<Setter Property="Height" Value="auto"/>
<Style.Resources>
<Storyboard x:Key="TransformImage">
<DoubleAnimation Duration="0:0:3" By="-500" Storyboard.TargetProperty="(Image.RenderTransform).(TranslateTransform.X)"></DoubleAnimation>
<DoubleAnimation Duration="0:0:3" By="-500" Storyboard.TargetProperty="(Image.RenderTransform).(TranslateTransform.Y)"></DoubleAnimation>
<DoubleAnimation Duration="0:0:3" By="-500" From="1" To="0.5" Storyboard.TargetProperty="(Image.RenderTransform).(ScaleTransform.ScaleX)"></DoubleAnimation>
<DoubleAnimation Duration="0:0:3" By="-500" From="1" To="0.5" Storyboard.TargetProperty="(Image.RenderTransform).(ScaleTransform.ScaleY)"></DoubleAnimation>
</Storyboard>
</Style.Resources>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource TransformImage}" />
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
答案 0 :(得分:1)
你需要做两件事才能让它发挥作用。首先定义您想要设置的默认变换组(在您的情况下,在变换组内部进行相应的变换)
<Setter Property="RenderTransform">
<Setter.Value>
<TransformGroup>
<TranslateTransform />
<ScaleTransform />
</TransformGroup>
</Setter.Value>
</Setter>
然后,当您实际引用目标属性时,必须明确指定要更改的变换组的点。我还建议添加一些退出操作,这样你的图像不会永远消失=)
<DoubleAnimation Duration="0:0:3" By="-500" Storyboard.TargetProperty="(Image.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)"></DoubleAnimation>
<DoubleAnimation Duration="0:0:3" By="-500" Storyboard.TargetProperty="(Image.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)"></DoubleAnimation>
<DoubleAnimation Duration="0:0:3" By="-500" From="1" To="0.5" Storyboard.TargetProperty="(Image.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"></DoubleAnimation>
<DoubleAnimation Duration="0:0:3" By="-500" From="1" To="0.5" Storyboard.TargetProperty="(Image.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"></DoubleAnimation>