Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I got tired of having to fiddle with XAML to animate grid rows and columns a while ago so I wrote a couple of methods to do it totally from code. </p> <p>With these you can expand/shrink columns and rows from code with one line:</p> <pre><code>Animation.AnimationHelper.AnimateGridColumnExpandCollapse(LeftColumn, true, expandedHeight, currentWidth, LeftColumn.MinWidth, 0, 200); </code></pre> <p>One important thing to note is setting the animation to null on completion. If you don't do this, the grid is still under control of the animation when the animation is complete. This might be fine if the grid doesn't have a splitter, but if the grid has a splitter and you want to be able to resize it manually after the animation completes, then you have to set the animation to null after it completes. </p> <p>Here are the methods:</p> <pre><code> /// &lt;summary&gt; /// Animate expand/collapse of a grid column. /// &lt;/summary&gt; /// &lt;param name="gridColumn"&gt;The grid column to expand/collapse.&lt;/param&gt; /// &lt;param name="expandedWidth"&gt;The expanded width.&lt;/param&gt; /// &lt;param name="milliseconds"&gt;The milliseconds component of the duration.&lt;/param&gt; /// &lt;param name="collapsedWidth"&gt;The width when collapsed.&lt;/param&gt; /// &lt;param name="minWidth"&gt;The minimum width of the column.&lt;/param&gt; /// &lt;param name="seconds"&gt;The seconds component of the duration.&lt;/param&gt; /// &lt;param name="expand"&gt;If true, expand, otherwise collapse.&lt;/param&gt; public static void AnimateGridColumnExpandCollapse(ColumnDefinition gridColumn, bool expand, double expandedWidth, double collapsedWidth, double minWidth, int seconds, int milliseconds) { if( expand &amp;&amp; gridColumn.ActualWidth &gt;= expandedWidth) // It's as wide as it needs to be. return; if (!expand &amp;&amp; gridColumn.ActualWidth == collapsedWidth) // It's already collapsed. return; Storyboard storyBoard = new Storyboard(); GridLengthAnimation animation = new GridLengthAnimation(); animation.From = new GridLength(gridColumn.ActualWidth); animation.To = new GridLength(expand ? expandedWidth : collapsedWidth); animation.Duration = new TimeSpan(0, 0, 0, seconds, milliseconds); // Set delegate that will fire on completion. animation.Completed += delegate { // Set the animation to null on completion. This allows the grid to be resized manually gridColumn.BeginAnimation(ColumnDefinition.WidthProperty, null); // Set the final value manually. gridColumn.Width = new GridLength(expand ? expandedWidth : collapsedWidth); // Set the minimum width. gridColumn.MinWidth = minWidth; }; storyBoard.Children.Add(animation); Storyboard.SetTarget(animation, gridColumn); Storyboard.SetTargetProperty(animation, new PropertyPath(ColumnDefinition.WidthProperty)); storyBoard.Children.Add(animation); // Begin the animation. storyBoard.Begin(); } /// &lt;summary&gt; /// Animate expand/collapse of a grid row. /// &lt;/summary&gt; /// &lt;param name="gridRow"&gt;The grid row to expand/collapse.&lt;/param&gt; /// &lt;param name="expandedHeight"&gt;The expanded height.&lt;/param&gt; /// &lt;param name="collapsedHeight"&gt;The collapesed height.&lt;/param&gt; /// &lt;param name="minHeight"&gt;The minimum height.&lt;/param&gt; /// &lt;param name="milliseconds"&gt;The milliseconds component of the duration.&lt;/param&gt; /// &lt;param name="seconds"&gt;The seconds component of the duration.&lt;/param&gt; /// &lt;param name="expand"&gt;If true, expand, otherwise collapse.&lt;/param&gt; public static void AnimateGridRowExpandCollapse(RowDefinition gridRow, bool expand, double expandedHeight, double collapsedHeight, double minHeight, int seconds, int milliseconds) { if (expand &amp;&amp; gridRow.ActualHeight &gt;= expandedHeight) // It's as high as it needs to be. return; if (!expand &amp;&amp; gridRow.ActualHeight == collapsedHeight) // It's already collapsed. return; Storyboard storyBoard = new Storyboard(); GridLengthAnimation animation = new GridLengthAnimation(); animation.From = new GridLength(gridRow.ActualHeight); animation.To = new GridLength(expand ? expandedHeight : collapsedHeight); animation.Duration = new TimeSpan(0, 0, 0, seconds, milliseconds); // Set delegate that will fire on completioon. animation.Completed += delegate { // Set the animation to null on completion. This allows the grid to be resized manually gridRow.BeginAnimation(RowDefinition.HeightProperty, null); // Set the final height. gridRow.Height = new GridLength(expand ? expandedHeight : collapsedHeight); // Set the minimum height. gridRow.MinHeight = minHeight; }; storyBoard.Children.Add(animation); Storyboard.SetTarget(animation, gridRow); Storyboard.SetTargetProperty(animation, new PropertyPath(RowDefinition.HeightProperty)); storyBoard.Children.Add(animation); // Begin the animation. storyBoard.Begin(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload