如何在ControlTemplate中获取动画资源?

时间:2011-05-01 16:11:47

标签: wpf

...

 <ControlTemplate TargetType="{x:Type CheckBox}">
                <ControlTemplate.Resources>
                    <Storyboard x:Key="OnChecking">
                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                       Storyboard.TargetName="slider"
                                                       Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                            <SplineDoubleKeyFrame x:Name="SplineValue"
                                                  KeyTime="00:00:00.3000000"
                                                  Value="25" />
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                    <Storyboard x:Key="OnUnchecking">
                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                       Storyboard.TargetName="slider"
                                                       Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                            <SplineDoubleKeyFrame KeyTime="00:00:00.3000000"
                                                  Value="0" />
                        </DoubleAnimationUsingKeyFrames>
                        <ThicknessAnimationUsingKeyFrames BeginTime="00:00:00"
                                                          Storyboard.TargetName="slider"
                                                          Storyboard.TargetProperty="(FrameworkElement.Margin)">
                            <SplineThicknessKeyFrame KeyTime="00:00:00.3000000"
                                                     Value="1,1,1,1" />
                        </ThicknessAnimationUsingKeyFrames>
                    </Storyboard>
                </ControlTemplate.Resources>

...

我可以使用下面的语句在后面的代码中获取资源“OnChecking”。

Storyboard stb1 = this.Template.Resources["OnChecking"] as Storyboard;

但是如何才能在故事板中获得“SplineValue”SplineDoubleKeyFrame?

1 个答案:

答案 0 :(得分:1)

这应该有效

 Storyboard stb1 = chk.Template.Resources["OnChecking"] as Storyboard;
 DoubleAnimationUsingKeyFrames animation =
                       (DoubleAnimationUsingKeyFrames)stb1.Children[0];
 var val = ((SplineDoubleKeyFrame)animation.KeyFrames[0]).Value;