繁体   English   中英

c#wpf加载时将DoubleAnimation“ To”更改为 <Window.Resources> )

[英]c# wpf change DoubleAnimation “To” when load (in <Window.Resources>)

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

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更改To其他值,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM