2012-05-03 85 views
2

我想制作一个TranslateTransform动画。在动画中,我需要我的对象留在窗口的中心。我知道WPF动画是Freezable。我正在使用转换器,但它在启动时初始化值。有什么办法可以在运行时设置EasingDoubleKeyFrame的值吗?如何在运行时更改EasingDoubleKeyFrame值?

这里是我的XAML代码:

<Storyboard x:Key="Storyboard1"> 
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="grid"> 
     <EasingDoubleKeyFrame KeyTime="0" Value="{Binding Width, Converter={StaticResource Minus}, ElementName=grid}"/> 
     <EasingDoubleKeyFrame KeyTime="0:0:1" Value="{Binding Width, Converter={StaticResource EnteringValueConverter}, ElementName=grid}"/> 
    </DoubleAnimationUsingKeyFrames> 
</Storyboard> 

回答

0

添加X:名称属性的EasingDoubleKeyFrame元素。 之后,你应该能够通过代码来访问它:

X:NAME = “关键帧”

然后:

keyframe.SetValue(EasingDoubleKeyFrame.ValueProperty,yourValue);

+0

我将x:Name属性添加到EasingDoubleKeyFrame元素,但我无法从后面的代码中访问它。 在后面的代码中没有任何名称为myEasingKey。 –

0

“这是因为动画冻结的对象。有一个在MSDN Documentation的更多信息,但基本上这意味着你不能用绑定,因为在冻结对象的属性(即动画)不能改变。

要避开这个限制,你需要在代码隐藏方面做一些或所有的工作。“从Stackoverflow

0

EasingDoubleKeyFrame

报价不能通过x:Name属性进行访问。相反,你应该使用不同的方法来定义EasingDoubleKeyFrameStaticResource并通过ResourceKey这样的链接是:通过后面的代码

<UserControl.Resources> 
    <EasingDoubleKeyFrame x:Key="myEasingKey" KeyTime="0:0:2" Value="136" /> 
    <Storyboard x:Key="Storyboard1"> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" 
             Storyboard.TargetName="rectangle"> 
      <StaticResource ResourceKey="myEasingKey" /> 
     </DoubleAnimationUsingKeyFrames> 
    </Storyboard> 
</UserControl.Resources> 

访问它象下面这样:

(EasingDoubleKeyFrame)Resources["myEasingKey"].Value = X; 

希望这有助于...