如何在代码隐藏的WPF中同时为高度和宽度设置动画?

时间:2012-07-16 12:03:32

标签: wpf vb.net forms

我正在尝试同时为表单(MainWnd)的WidthHeight制作动画,但我无法获得所需的输出。

    Dim aniHeight As New DoubleAnimation
    Dim aniWidth As New DoubleAnimation
    'story board to handle 2 animations
    Dim sb As New Storyboard()
    aniWidth.From = Me.Width
    aniHeight.From = Me.Height
    aniHeight.To = 470
    aniWidth.To = 500
    aniHeight.Duration = TimeSpan.FromSeconds(1)
    aniWidth.Duration = TimeSpan.FromSeconds(1)
    sb.Children.Add(aniHeight)
    sb.Children.Add(aniWidth)
    Storyboard.SetTarget(aniHeight, MainWnd)
    Storyboard.SetTarget(aniWidth, MainWnd)
    Storyboard.SetTargetProperty(aniWidth, New PropertyPath("(Width)"))
    Storyboard.SetTargetProperty(aniHeight, New PropertyPath("(Height)"))
    sb.Begin()

1 个答案:

答案 0 :(得分:0)

在这里,您可以使用以下代码进行补偿。

<Style x:Key="InvViewBoxStyle" TargetType="Viewbox">    
    <Setter Property="RenderTransform">    
        <Setter.Value>    
            <ScaleTransform />    
        </Setter.Value>    
    </Setter>    
    <Style.Triggers>    
        <DataTrigger Binding="{Binding IsChecked, ElementName=myCheckBox}" Value="True">    
            <DataTrigger.EnterActions>    
                <BeginStoryboard>    
                    <Storyboard>    
                        <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(ScaleTransform.ScaleY)" To="2" Duration="0:0:0.5" />    
                        <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(ScaleTransform.ScaleY)" To="2" Duration="0:0:0.5" />    
                    </Storyboard>    
                </BeginStoryboard>    
            </DataTrigger.EnterActions>    
        </DataTrigger>    
    </Style.Triggers>    
</Style>