[Silverlight] コード側からStoryBoardを生成

7:34 PM on 2010年12月10日 ,

// ボタンを拡大表示するStoryboard生成
private void CreateStoryboard(Button btn)
{
    Storyboard sb = new Storyboard();

    // 幅変更DoubleAnimation
    DoubleAnimation animationWidth = new DoubleAnimation()
    {
        Duration = TimeSpan.FromSeconds(1.0),
        From = 50.0,
        To = 100.0,
        EasingFunction = new BackEase()
        {
            Amplitude = 1,
            EasingMode = EasingMode.EaseInOut,
        }
    };

    // 高さ変更DoubleAnimation
    DoubleAnimation animationHeight = new DoubleAnimation()
    {
        Duration = TimeSpan.FromSeconds(1.0),
        From = 50.0,
        To = 100.0,
        EasingFunction = new BackEase()
        {
            Amplitude = 1,
            EasingMode = EasingMode.EaseInOut,
        }
    };

    // DoubleAnimationに対象のオブジェクトを設定
    Storyboard.SetTarget(animationWidth, btn);
    Storyboard.SetTarget(animationHeight, btn);

    // DoubleAnimationで変化させるプロパティを設定
    //Storyboard.SetTargetProperty(animationWidth, new PropertyPath("(FrameworkElement.Width)"));
    //Storyboard.SetTargetProperty(animationHeight, new PropertyPath("(FrameworkElement.Height)"));
    Storyboard.SetTargetProperty(animationWidth, new PropertyPath(FrameworkElement.WidthProperty));
    Storyboard.SetTargetProperty(animationHeight, new PropertyPath(FrameworkElement.HeightProperty));

    // StoryboardにDoubleAnimation追加
    sb.Children.Add(animationWidth);
    sb.Children.Add(animationHeight);

    // Storyboard開始
    sb.Begin();
}

----------

// FrameworkElementが上から降りてくるStoryboard生成
private Storyboard CreateStoryboard(FrameworkElement elem)
{
    Storyboard sb = new Storyboard();

    DoubleAnimationUsingKeyFrames keyFrames = new DoubleAnimationUsingKeyFrames();

    EasingDoubleKeyFrame keyFrame1 = new EasingDoubleKeyFrame()
    {
        KeyTime = TimeSpan.FromSeconds(0.0),
        Value = -50.0
    };

    EasingDoubleKeyFrame keyFrame2 = new EasingDoubleKeyFrame()
    {
        KeyTime = TimeSpan.FromSeconds(1.0),
        Value = 0,
        EasingFunction = new BackEase()
        {
            Amplitude = 1,
            EasingMode = EasingMode.EaseInOut
        }
    };

    EasingDoubleKeyFrame keyFrame3 = new EasingDoubleKeyFrame()
    {
        KeyTime = TimeSpan.FromSeconds(1.5),
        Value = 0
    };

    keyFrames.KeyFrames.Add(keyFrame1);
    keyFrames.KeyFrames.Add(keyFrame2);
    keyFrames.KeyFrames.Add(keyFrame3);

    Storyboard.SetTarget(keyFrames, elem);
    Storyboard.SetTargetProperty(keyFrames, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)"));

    sb.Children.Add(keyFrames);

    return sb;
}