2016-07-25 6 views
1

私はUnable to set the property after its animationですので、私のターゲットプロパティからアニメーションを削除したいと思います。完了後にアニメーションを削除するには

上記の質問のanswerによれば、完成後にアニメーションを削除する必要があります。しかしどうすれば?ここで

は私のコードです:それは完了だ後、私はアニメーションを削除することができ

GridLengthAnimation gridAnim = new GridLengthAnimation() { 
    Name = "gridAnim", 
    Target = mainGrid.ColumnDefinitions[1], 
    Duration = new Duration(TimeSpan.FromMilliseconds(1000)) 
}; 

gridAnim.From = new GridLength(1, GridUnitType.Star); 
gridAnim.To = new GridLength(0, GridUnitType.Star); 

gridAnim.BeginAnimation(); 

public class GridLengthAnimation : AnimationTimeline { 

    static GridLengthAnimation() { 
     FromProperty = DependencyProperty.Register("From", typeof(GridLength), typeof(GridLengthAnimation)); 
     ToProperty = DependencyProperty.Register("To", typeof(GridLength), typeof(GridLengthAnimation)); 
    } 

    public override Type TargetPropertyType { 
     get { return typeof(GridLength); } 
    } 

    protected override Freezable CreateInstanceCore() { 
     return new GridLengthAnimation(); 
    } 

    public ContentElement Target { get; set; } 

    public static readonly DependencyProperty FromProperty; 
    public GridLength From { 
     get { return (GridLength) GetValue(FromProperty); } 
     set { SetValue(FromProperty, value); } 
    } 

    public static readonly DependencyProperty ToProperty; 
    public GridLength To { 
     get { return (GridLength) GetValue(ToProperty); } 
     set { SetValue(ToProperty, value); } 
    } 

    public void BeginAnimation() { 
     Target.BeginAnimation(ColumnDefinition.WidthProperty, null); 
     Target.BeginAnimation(ColumnDefinition.WidthProperty, this); 
    } 

    public IEasingFunction EasingFunction { get; set; } = new CubicEase() { EasingMode = EasingMode.EaseInOut }; 

    public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock) { 
     double fromValue = ((GridLength) GetValue(FromProperty)).Value; 
     double toValue = ((GridLength) GetValue(ToProperty)).Value; 
     double newValue = 0; 

     if ((fromValue > toValue)) { 
      newValue = (1 - EasingFunction.Ease(animationClock.CurrentProgress.Value)) * (fromValue - toValue) + toValue; 
      return new GridLength(newValue, GridUnitType.Star); 
     } else { 
      newValue = EasingFunction.Ease(animationClock.CurrentProgress.Value) * (toValue - fromValue) + fromValue; 
      return new GridLength(newValue, GridUnitType.Star); 
     } 
    } 
} 

アニメーションを開始するには、私は私のMainWindows.xaml.csファイルで、このようにそれを行いますか?

+1

愚かな質問を、しかし、どこで、どのようにそのアニメーションを設定するのですか? – lokusking

+0

@ lokusking私の質問を編集しました:) – Drarig29

答えて

0

私はに「個々のプロパティからアニメーションを削除」の部分を、私の解決策hereを見つけました。

私は私のGridLengthAnimationでこれを追加しました:たぶん

public void BeginAnimation() { 
    Completed += GridLengthAnimation_Completed; 
    Target.BeginAnimation(ColumnDefinition.WidthProperty, null); 
    Target.BeginAnimation(ColumnDefinition.WidthProperty, this); 
} 

private void GridLengthAnimation_Completed(object sender, EventArgs e) { 
    //unlocks the property 
    Target.BeginAnimation(ColumnDefinition.WidthProperty, null); 

    //holds the value at the end 
    ((ColumnDefinition) Target).Width = new GridLength(((GridLength) GetValue(ToProperty)).Value, GridUnitType.Star); 
} 
1

だけに使用状況を変更します。

GridLengthAnimation gridAnim = new GridLengthAnimation() { 
      Name = "gridAnim", 
      Target = this.MainGrid.ColumnDefinitions[1], 
      Duration = new Duration(TimeSpan.FromMilliseconds(1000)) 
      }; 

      gridAnim.From = new GridLength(1, GridUnitType.Star); 
      gridAnim.To = new GridLength(0, GridUnitType.Star); 
//Cleanup on Complete 
      gridAnim.Completed += (sender, args) => { 
                var xx = sender as AnimationClock; 
                (xx.Timeline as GridLengthAnimation).Target = null; 
      }; 
      gridAnim.BeginAnimation(); 
+0

申し訳ありませんが動作しません...私はそれが良いアイデアだと思いますが、プロパティはまだロックされています。別の方法がありますか? – Drarig29

関連する問題