2014-02-23 4 views
1

내 창에 Button이 있는데 어떻게 C#에서 코드를 사용하면 왼쪽으로 시프트하도록 애니메이션을 적용 할 수 있습니까? 그것.System.Windows.Media.Animation을 사용하여 단추 애니메이션 이동

private void myButton_Click(object sender, System.Windows.RoutedEventArgs e) 
{ 
    // TODO: Add event handler implementation here. 
    DoubleAnimation _DoubleAnimation = new DoubleAnimation(); 
    _DoubleAnimation.From = 0; 
    _DoubleAnimation.To = 100; 
    _DoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); 

    myButton.BeginAnimation(Button.RenderTransformOriginProperty, _DoubleAnimation); 
} 

답변

3

당신은 애니메이션 WidthProperty해야합니다이 경우, 크기를 변경하려면 :

myButton.BeginAnimation(Button.WidthProperty, _DoubleAnimation); 

는 애니메이션 날카로운 전환을 방지하기 위해, 당신은 명시 적으로 폭 설정해야합니다 the Button :

<Button Name="myButton" 
     Width="30" 
     Content="Test" 
     Click="myButton_Click" /> 

그리고 애니메이션에서 단지 To 값 : 당신이 애니메이션에 Button을 이동하려면

private void myButton_Click(object sender, System.Windows.RoutedEventArgs e) 
{ 
    DoubleAnimation _DoubleAnimation = new DoubleAnimation(); 
    _DoubleAnimation.To = 100; 
    _DoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); 

    myButton.BeginAnimation(Button.WidthProperty, _DoubleAnimation); 
} 

, 당신은 ThicknessAnimation 사용할 수 있습니다

감사 발언에 대한 @Rohit Vats

private void myButton_Click(object sender, System.Windows.RoutedEventArgs e) 
{ 
    ThicknessAnimation animation = new ThicknessAnimation(new Thickness(0), 
                  new Thickness(100, 0, 0, 0), 
                  new Duration(new TimeSpan(0, 0, 1)), 
                  FillBehavior.HoldEnd); 

    myButton.BeginAnimation(Button.MarginProperty, animation); 
} 
+1

이봐 @Anatoily 내가 OP를 추측 버튼의 폭 재생하지 않고 100 픽셀 왼쪽에 버튼을 이동하려는. 적어도 내가이 질문에서 추론 할 수있는 것. 그래서, 내 대답뿐만 아니라 단추를 이동하려면 게시 된 경우 너비가 필요합니다 귀하의 솔루션을 잘 작동합니다. +1. :) –

+0

@Rohit Vats : 네, 질문을 이해하지 못합니다, 죄송합니다. 당신의 솔루션은 OP에 더 적합합니다. –

2

에 당신이 버튼을 이동하려면 경우 왼쪽으로 100 픽셀, 왼쪽으로의 마진을 -100으로 설정해야합니다. 분명히 은 DoubleAnimation에 의해 달성 될 수 없지만 여백은 두께 유형 인 반면 이중 값만 애니메이션 할 수 있습니다.

당신은 달성 할 수 사용 Storyboard :

Storyboard myStoryboard = new Storyboard(); 
ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames(); 
animation.BeginTime = TimeSpan.FromSeconds(0); 
Storyboard.SetTarget(animation, myButton); 
Storyboard.SetTargetProperty(animation, new PropertyPath("Margin")); 
double left = myButton.Margin.Left - 100; 
DiscreteObjectKeyFrame keyFrame = new DiscreteObjectKeyFrame(new Thickness(left, 
              0, 0, 0), TimeSpan.FromSeconds(1)); 
animation.KeyFrames.Add(keyFrame); 
myStoryboard.Children.Add(animation); 
myStoryboard.Begin(); 

원활한 전환를 들어 당신이 할 수있는 것은 여러 키 프레임을 추가하고 0.1 초의 간격으로 재생할 수 있습니다. 보다 원활한 전환이 필요한 경우 더 작은 간격으로 나누십시오.

이것은 당신이 그것을하는 방법이다 :

Storyboard myStoryboard = new Storyboard(); 
ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames(); 
animation.BeginTime = TimeSpan.FromSeconds(0); 
Storyboard.SetTarget(animation, myButton); 
Storyboard.SetTargetProperty(animation, new PropertyPath("Margin")); 
for (int count = 1; count < 21; count++) 
{ 
    double left = myButton.Margin.Left - (5 * count); 
    DiscreteObjectKeyFrame keyFrame = new DiscreteObjectKeyFrame(new 
        Thickness(left, 0, 0, 0), TimeSpan.FromSeconds(0.05 * count)); 
    animation.KeyFrames.Add(keyFrame); 
} 
myStoryboard.Children.Add(animation); 
myStoryboard.Begin(); 
+0

안녕하세요. 애니메이션이 시작되면 1 초 후에 한 지점에서 예상 끝점으로 점프합니다. 나는 부드러운 전환을 원한다. – RStyle

+0

@RStyle - 순조로운 전환을위한 대답은 업데이트를 참조하십시오. –

+0

위대한 issit는 xml에서 더 단순 해 보이기 때문에 이중 애니메이션을 사용할 수 없습니다. C#에서 사용할 수있을 것 같았습니다. – RStyle

1

그것이 당신이 페이지에 Button 배치 얼마나 따라 달라집니다. 좌표로 요소를 배치하려는 경우 (권장하지 않음) Canvas에 단추를 배치 할 수 있습니다. 코드 숨김

<Canvas> 
    <Button Canvas.Left="100" Canvas.Top="100" Name="btnOk" HorizontalAlignment="Center" VerticalAlignment="Center" Width="50" Click="BtnOk_OnClick">Ok</Button> 
</Canvas> 

과 : 그럼 당신은 다음과 같은 코드를 사용할 수 있습니다

var doubleAnimation = new DoubleAnimation(); 
double left = Canvas.GetLeft(btnOk); 
doubleAnimation.From = left; 
doubleAnimation.To = left - 100; 
doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); 
btnOk.BeginAnimation(Canvas.LeftProperty, doubleAnimation);