使用故事板更改矩形的比例

时间:2014-03-06 14:24:49

标签: c# wpf xaml storyboard

我想用故事板改变Rectangle的比例,我不知道为什么它不起作用。期待任何人的回复! C#代码是:

Storyboard sb = new Storyboard();
InitializeComponent();
DoubleAnimation daScaleX = new DoubleAnimation();
daScaleX.From = 1;
daScaleX.To = 2;
daScaleX.Duration = TimeSpan.FromSeconds(3);

DoubleAnimation daScaleY = new DoubleAnimation();
daScaleY.From = 1;
daScaleY.To = 2;
daScaleY.Duration = TimeSpan.FromSeconds(3);

BounceEase easing = new BounceEase()
{
    EasingMode = EasingMode.EaseOut
};

daScaleX.EasingFunction = easing;
daScaleY.EasingFunction = easing;

Storyboard.SetTargetProperty(daScaleX, new PropertyPath("ScaleX"));
Storyboard.SetTarget(daScaleX, st);
Storyboard.SetTargetProperty(daScaleY, new PropertyPath("ScaleY"));
Storyboard.SetTarget(daScaleY, st);

sb.Children.Add(daScaleX);
sb.Children.Add(daScaleY);
sb.Begin();

XAML文件是:

<Grid>
    <StackPanel>
      <Rectangle x:Name="rect" Fill="Blue" Width="200" Height="40" Margin="5" RenderTransformOrigin="0.5,0.5">
        <Rectangle.RenderTransform>
            <TransformGroup>
                <ScaleTransform x:Name="st" ScaleX="1" ScaleY="1" />
            </TransformGroup>
        </Rectangle.RenderTransform>
      </Rectangle>
    </StackPanel>
</Grid>

1 个答案:

答案 0 :(得分:0)

单独尝试以下代码..这对我有用

   <Grid>
        <StackPanel>
            <Rectangle x:Name="rect" Fill="Blue" Width="200" Height="40" Margin="5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <ScaleTransform x:Name="st" ScaleX="1.0" ScaleY="1.0"></ScaleTransform>
                </Rectangle.RenderTransform>
            </Rectangle>
        </StackPanel>
    </Grid>

并在Windows加载的事件

上完成了这个动画
void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Storyboard sb = new Storyboard();   

        DoubleAnimation daScaleX = new DoubleAnimation();
        daScaleX.From = 1;
        daScaleX.To = 2;
        daScaleX.Duration = TimeSpan.FromMilliseconds(300);

        DoubleAnimation daScaleY = new DoubleAnimation();
        daScaleY.From = 1;
        daScaleY.To = 2;
        daScaleY.Duration = TimeSpan.FromMilliseconds(300);

        BounceEase easing = new BounceEase()
        {
            EasingMode = EasingMode.EaseOut
        };

        daScaleX.EasingFunction = easing;
        daScaleY.EasingFunction = easing;

        Storyboard.SetTargetProperty(daScaleX, new PropertyPath("RenderTransform.ScaleX"));
        Storyboard.SetTarget(daScaleX, rect);
        Storyboard.SetTargetProperty(daScaleY, new PropertyPath("RenderTransform.ScaleY"));
        Storyboard.SetTarget(daScaleY, rect);

        sb.Children.Add(daScaleX);
        sb.Children.Add(daScaleY);
        sb.Begin();
    }