如何在Silverlight中为多个控件使用一个动画

时间:2012-02-12 23:00:51

标签: silverlight

我只是在研究Silverlight(在C#中),我遇到了问题。 我有14个矩形,它们的宽度/高度设置为“自动”。我想在MouseEnter / MouseLeave上创建动画。动画只会改变宽度/高度。我使用Expression Blend for Silverlight 5做到了,一切都很好,但它制作了巨大的代码。我可以为所有矩形制作一些资源动画吗?

这是Blend制作的XAML代码,我有14个这样的块

<Storyboard x:Name="BigTableOne_MouseEnter">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Storyboard.TargetName="Table1">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="1.04"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)" Storyboard.TargetName="Table1">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="1.04"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Name="BigTableOne_MouseLeave">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Storyboard.TargetName="Table1">
            <EasingDoubleKeyFrame KeyTime="0" Value="1.04"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)" Storyboard.TargetName="Table1">
            <EasingDoubleKeyFrame KeyTime="0" Value="1.04"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>

1 个答案:

答案 0 :(得分:0)

除了“Storyboard.TargetName”属性之外,您的所有故事板都具有相同的内容。因此,您可以删除此属性并在运行时动态设置它。

您可以找到更多详细信息here