2012-02-20 23 views
6

我是wpf的初學者,今天我遇到了一個奇怪的問題,即如果向網格列添加動畫,分離器停止工作,下面是代碼片段,這是一個毫無意義的,但很簡單的測試代碼,它什麼也不做,當鼠標進入右側立柱除了,它的寬度將擴大15至100將GridSplitter應用到網格列後不工作

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Name="c1"/> 
     <ColumnDefinition Name="c2" Width="15" MinWidth="15"/> 
    </Grid.ColumnDefinitions> 
    <Border Grid.Column="0" Background="Gray"></Border> 
    <GridSplitter Width="8" Background="Yellow"></GridSplitter> 
    <Border Grid.Column="1" Background="Silver" Name="bdRight" MouseEnter="bdRight_MouseEnter"></Border> 
</Grid> 

這:

 bool flag = true; 
    private void bdRight_MouseEnter(object sender, MouseEventArgs e) 
    { 
     if (flag) 
     { 
      flag = false; 
      var da = new GridLengthAnimation(); 
      da.From = new GridLength(c2.MinWidth); 
      da.To = new GridLength(100); 
      var ef = new BounceEase(); 
      ef.EasingMode = EasingMode.EaseOut; 
      da.EasingFunction = ef; 
      this.c2.BeginAnimation(ColumnDefinition.WidthProperty, da); 
     } 
    } 

和這裏的GridLengthAnimation,我在找到DoubleAnimati後從互聯網上獲得它on不能用於抵抗柵欄的寬度。

public class GridLengthAnimation : AnimationTimeline 
{ 
    public static readonly DependencyProperty FromProperty; 
    public static readonly DependencyProperty ToProperty; 
    public static readonly DependencyProperty EasingFunctionProperty; 

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

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

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

    public IEasingFunction EasingFunction 
    { 
     get 
     { 
      return (IEasingFunction)GetValue(GridLengthAnimation.EasingFunctionProperty); 
     } 
     set 
     { 
      SetValue(GridLengthAnimation.EasingFunctionProperty, value); 
     } 

    } 

    public GridLength From 
    { 
     get 
     { 
      return (GridLength)GetValue(GridLengthAnimation.FromProperty); 
     } 
     set 
     { 
      SetValue(GridLengthAnimation.FromProperty, value); 
     } 
    } 

    public GridLength To 
    { 
     get 
     { 
      return (GridLength)GetValue(GridLengthAnimation.ToProperty); 
     } 
     set 
     { 
      SetValue(GridLengthAnimation.ToProperty, value); 
     } 
    } 

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

     IEasingFunction easingFunction = this.EasingFunction; 

     double progress = (easingFunction != null) ? easingFunction.Ease(animationClock.CurrentProgress.Value) : animationClock.CurrentProgress.Value; 

     if (fromValue > toValue) 
     { 
      return new GridLength((1 - progress) * (fromValue - toValue) + toValue, this.To.IsStar ? GridUnitType.Star : GridUnitType.Pixel); 
     } 
     else 
     { 
      return new GridLength((progress) * (toValue - fromValue) + fromValue, this.To.IsStar ? GridUnitType.Star : GridUnitType.Pixel); 
     } 
    } 
} 

如果我註釋掉的MouseEnter事件處理程序代碼,分路器工作正常,否則,它停止工作。有任何想法嗎?

回答

8

您必須設置動畫的FillBehavior屬性。默認值爲HoldEnd,這意味着動畫在結束後保留​​最終值。如果將FillBehavior設置爲Stop,則動畫值將恢復爲動畫前的值。

如果添加以下行到你的事件處理程序代碼,應該按預期工作:

... 
da.FillBehavior = FillBehavior.Stop; 
c2.Width = da.To; // set final value before starting the animation 
c2.BeginAnimation(ColumnDefinition.WidthProperty, da); 

如果啓動動畫之前設定的最終值創建一個閃爍效果,你可以改爲在Completed處理程序設定終值:

... 
da.FillBehavior = FillBehavior.Stop; 
da.Completed += (s, e) => c2.Width = da.To; 
c2.BeginAnimation(ColumnDefinition.WidthProperty, da); 
+1

血腥冷靜,它確實工作,謝謝。 – morven 2012-02-20 10:11:28

1

只是對剛纔的答覆擴大(就發表評論,但不是由計算器征服者允許):

對於我來說,設置拉開動畫之前終值生成我網第二分割動畫之前踢在一個醜陋的閃爍效果。

我所做的就是用FillBehavior.Stop上述但訂閱Animation.Completed事件並在那裏設置網格列的最終高度。

工作像魅力沒有任何閃爍。