【问题标题】:Grid Column changing Width when animating动画时网格列更改宽度
【发布时间】:2011-05-13 15:02:55
【问题描述】:

我有一个 Grid 元素,它有两列和三行。最后一行的高度为 0...我使用自定义动画类为 height 属性设置动画,因为 gridheight 属性不是整数..

动画效果很好,但是当我激活它时,它似乎随机地改变了第二列的宽度。有时只是大几个像素,有时是宽度的两倍……

这里是网格代码

<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>

有什么原因会发生这种情况吗?

【问题讨论】:

    标签: wpf animation grid row


    【解决方案1】:

    对于那些可能想知道问题中提到的 GridLengthAnimation 实现的人,这里有一个(来自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);
        }
    }
    

    【讨论】:

    • 谢谢!我不得不将 fromVal = ((GridLength)defaultDestinationValue).Value; 更改为 fromVal = ((GridLength)defaultOriginValue).Value;。但除此之外,它是完美的。
    【解决方案2】:

    这可能取决于“单元格”的内容

    除了 Width="50" 之外,尝试在 columnDefinition 上设置 MaxWidth="50"

    【讨论】:

      猜你喜欢
      • 2012-01-02
      • 1970-01-01
      • 2021-02-01
      • 2013-06-20
      • 2019-12-21
      • 1970-01-01
      • 2018-01-13
      • 2021-02-23
      • 1970-01-01
      相关资源
      最近更新 更多