2010-11-22 22 views
3

J'ai un élément Grid qui a deux colonnes et trois lignes. La dernière ligne a une hauteur de 0 ... et j'anime la propriété height en utilisant une classe d'animation personnalisée car la propriété gridheight n'est pas un nombre entier.Grid Changement de colonne Largeur lors de l'animation

L'animation fonctionne très bien, mais quand je l'active, elle change le largeur de la deuxième colonne apparemment au hasard .. parfois quelques pixels plus grands et parfois plus du double aussi large ...

Voici le code de grille

<Grid > 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition /> 
          <ColumnDefinition Width="50"/> 
         </Grid.ColumnDefinitions> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="1*" /> 
          <RowDefinition Height="7"/> 
          <RowDefinition Name="LyricsRow" Height="1"> 

           <RowDefinition.Style> 
            <Style> 
             <Style.Triggers> 
              <DataTrigger Binding="{Binding Path=IsTrayOpen}" Value="True"> 
               <DataTrigger.EnterActions> 
                <BeginStoryboard> 
                 <Storyboard> 
                  <local:GridLengthAnimation 
                   Storyboard.TargetProperty="Height" 
                   From="0" To="150" Duration="0:0:0.3" > 
                  </local:GridLengthAnimation> 
                 </Storyboard> 
                </BeginStoryboard> 
               </DataTrigger.EnterActions> 
               <DataTrigger.ExitActions> 
                <BeginStoryboard> 
                 <Storyboard> 
                  <local:GridLengthAnimation 
                   Storyboard.TargetProperty="Height" 
                   From="150" To="0" Duration="0:0:0.5" /> 
                 </Storyboard> 
                </BeginStoryboard> 
               </DataTrigger.ExitActions> 
              </DataTrigger> 
             </Style.Triggers> 
            </Style> 
           </RowDefinition.Style> 

          </RowDefinition> 
         </Grid.RowDefinitions> 

y at-il des raisons cela pourrait se passer?

Répondre

3

Cela dépend probablement du contenu des « cellules »

essayez de régler MaxWidth = « 50 » sur votre columnDefinition en plus Largeur = « 50 »

+0

Cela a fonctionné .. merci beaucoup – Ryan

6

Pour ceux d'entre vous qui peut-être sur la mise en œuvre de GridLengthAnimation mentionné dans la question, voici un (de http://social.msdn.microsoft.com/forums/en-US/wpf/thread/da47a4b8-4d39-4d6e-a570-7dbe51a842e4/)

/// <summary> 
/// Animates a grid length value just like the DoubleAnimation animates a double value 
/// </summary> 
public class GridLengthAnimation : AnimationTimeline 
{ 
    /// <summary> 
    /// Returns the type of object to animate 
    /// </summary> 
    public override Type TargetPropertyType 
    { 
     get 
     { 
      return typeof(GridLength); 
     } 
    } 

    /// <summary> 
    /// Creates an instance of the animation object 
    /// </summary> 
    /// <returns>Returns the instance of the GridLengthAnimation</returns> 
    protected override System.Windows.Freezable CreateInstanceCore() 
    { 
     return new GridLengthAnimation(); 
    } 

    /// <summary> 
    /// Dependency property for the From property 
    /// </summary> 
    public static readonly DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(GridLength), 
      typeof(GridLengthAnimation)); 

    /// <summary> 
    /// CLR Wrapper for the From depenendency property 
    /// </summary> 
    public GridLength From 
    { 
     get 
     { 
      return (GridLength)GetValue(GridLengthAnimation.FromProperty); 
     } 
     set 
     { 
      SetValue(GridLengthAnimation.FromProperty, value); 
     } 
    } 

    /// <summary> 
    /// Dependency property for the To property 
    /// </summary> 
    public static readonly DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(GridLength), 
      typeof(GridLengthAnimation)); 

    /// <summary> 
    /// CLR Wrapper for the To property 
    /// </summary> 
    public GridLength To 
    { 
     get 
     { 
      return (GridLength)GetValue(GridLengthAnimation.ToProperty); 
     } 
     set 
     { 
      SetValue(GridLengthAnimation.ToProperty, value); 
     } 
    } 

    /// <summary> 
    /// Animates the grid let set 
    /// </summary> 
    /// <param name="defaultOriginValue">The original value to animate</param> 
    /// <param name="defaultDestinationValue">The final value</param> 
    /// <param name="animationClock">The animation clock (timer)</param> 
    /// <returns>Returns the new grid length to set</returns> 
    public override object GetCurrentValue(object defaultOriginValue, 
     object defaultDestinationValue, AnimationClock animationClock) 
    { 
     double fromVal = ((GridLength)GetValue(GridLengthAnimation.FromProperty)).Value; 
     //check that from was set from the caller 
     if (fromVal == 1) 
      //set the from as the actual value 
      fromVal = ((GridLength)defaultDestinationValue).Value; 

     double toVal = ((GridLength)GetValue(GridLengthAnimation.ToProperty)).Value; 

     if (fromVal > toVal) 
      return new GridLength((1 - animationClock.CurrentProgress.Value) * (fromVal - toVal) + toVal, GridUnitType.Star); 
     else 
      return new GridLength(animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal, GridUnitType.Star); 
    } 
} 
+1

Merci! J'ai dû changer ** deVal = ((GridLength) defaultDestinationValue) .Value; ** à ** fromVal = ((GridLength) defaultOriginValue) .Value; **. Mais à part ça, c'est parfait. – Xtr