[Silverlight] コード側から生成するStoryBoardでMarginの値を変化させる例

9:53 PM on 2011年3月24日

private void CreateStoryboard(Button btn)
{
    Storyboard sb = new Storyboard();

    ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames()
    {
        Duration = TimeSpan.FromSeconds(1.0)
    };

    DiscreteObjectKeyFrame key1 = new DiscreteObjectKeyFrame();
    key1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0));
    key1.Value = new Thickness(0, 0, 0, 0);

    DiscreteObjectKeyFrame key2 = new DiscreteObjectKeyFrame();
    key2.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(500));
    key2.Value = new Thickness(50, 0, 0, 0);

    animation.KeyFrames.Add(key1);
    animation.KeyFrames.Add(key2);

    Storyboard.SetTarget(animation, btn);
    Storyboard.SetTargetProperty(animation, new PropertyPath(FrameworkElement.MarginProperty));

    sb.Children.Add(animation);
    sb.Begin();
}