0

我使用此代码从左到右为图像设置动画,但我希望将 To="100"设置为窗口宽度 如何在窗口加载或调整大小时更改 DoubleAnimation "To"?

xml:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" ResizeMode="CanResize" WindowState="Normal" WindowStyle="ToolWindow" Loaded="Window_Loaded">

  <Window.Resources>
    <Storyboard x:Key="PlayAnimation" Storyboard.TargetProperty="(Canvas.Left)">
        <DoubleAnimation   From="0" Duration="0:0:1" RepeatBehavior="Forever" To="100" />
    </Storyboard>
  </Window.Resources>

  <Grid>
    <Grid.Background>
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="White" Offset="1" />
        <GradientStop Color="#FF5AC0FF" Offset="0" />
      </LinearGradientBrush>
    </Grid.Background>


    <Canvas>            
      <Image  Cursor="Hand" Height="205" HorizontalAlignment="Left" Margin="74,78,0,0" Name="image2" Source="I:\a.png" Stretch="Fill" VerticalAlignment="Top" Width="200" />
    </Canvas>     
  </Grid>    
</Window>

C#代码:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
  Storyboard sb = this.FindResource("PlayAnimation") as Storyboard;        
  Storyboard.SetTarget(sb, this.image2);
  sb.Begin();
}
4

1 回答 1

0

如果我正确理解您的问题,您想To在代码隐藏中将 更改为一些不同的值,如下所示:

Storyboard sb = this.FindResource("PlayAnimation") as Storyboard;        
(sb.Children[0] as DoubleAnimation).To = 200;
Storyboard.SetTarget(sb, this.image2);
sb.Begin();
于 2014-11-15T10:22:20.920 回答