c# wpf 在加载时更改双动画"To"(在<Window.Resources>中)



我使用此代码从左到右对图像进行动画处理但我想 To="100" 到窗口宽度如何在窗口加载或调整大小时更改双动画"到"?

XAML :

<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();
}
如果我

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

Storyboard sb = this.FindResource("PlayAnimation") as Storyboard;        
(sb.Children[0] as DoubleAnimation).To = 200;
Storyboard.SetTarget(sb, this.image2);
sb.Begin();

相关内容

  • 没有找到相关文章

最新更新