如何为自定义组件旋转变换设置动画?

时间:2016-02-11 19:04:44

标签: c# wpf xaml storyboard transformation

鉴于以下内容:

<Viewbox>
    <Foo:Bar
        x:FieldModifier="private"
        x:Name="fooBar"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        RenderTransformOrigin="0.5,0.5">
        <Foo:Bar.RenderTransform>
            <TransformGroup>
                <ScaleTransform
                    x:FieldModifier="private"
                    x:Name="xfScale"/>
                <RotateTransform
                    x:FieldModifier="private"
                    x:Name="xfRotate"/>
            </TransformGroup>
        </Foo:Bar.RenderTransform>
        <Foo:Bar.Style>
            <Style TargetType="{x:Type Foo:Bar}">
                <Style.Triggers>
                    <DataTrigger
                        Binding="{
                            Binding Flip,
                            RelativeSource={
                                RelativeSource AncestorType={
                                    x:Type local:myFooBar}}}"
                        Value="True">
                        <DataTrigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetProperty=""/>
                                </Storyboard>
                            </BeginStoryboard>
                        </DataTrigger.EnterActions>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Foo:Bar.Style>
    </Foo:Bar>
</Viewbox>

对于一个新组件,它基本上是一个卡在ViewBox内部的花式标签(用于自动缩放标签),我需要指出Storyboard.TargetProperty能够设置动画,比方说, RotateTransform Angle属性?

1 个答案:

答案 0 :(得分:1)

需要为您的xfScale / xfRotate命名转换设置TargetName

您的TargetProperty将是所用变换的属性。

喜欢Scale;

Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"

Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"

除了仅指定属性外,您仍需要提供Value来设置动画。因此,就其而言,它会变成类似的东西;

<DoubleAnimationUsingKeyFrames Storyboard.TargetName="xfScale" 
                               Storyboard.TargetProperty="X">
   <SplineDoubleKeyFrame KeyTime="0:0:0.2" Value="0" />
</DoubleAnimationUsingKeyFrames>

或者对于轮换,您需要Angle属性。值得一提的是,Blend使这些东西比手动更快/更容易,特别是对于复杂的动画。

希望这会有所帮助,欢呼。