Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I built upon the <code>AnimationHelper</code> class provided by Nigel Shaw and wrapped it in a reusable <code>GridAnimationBehavior</code> which can be attached to the <code>RowDefinition</code> and <code>ColumnDefinition</code> elements.</p> <pre><code>/// &lt;summary&gt; /// Wraps the functionality provided by the &lt;see cref="AnimationHelper"/&gt; class /// in a behavior which can be used with the &lt;see cref="ColumnDefinition"/&gt; /// and &lt;see cref="RowDefinition"/&gt; types. /// &lt;/summary&gt; public class GridAnimationBehavior : DependencyObject { #region Attached IsExpanded DependencyProperty /// &lt;summary&gt; /// Register the "IsExpanded" attached property and the "OnIsExpanded" callback /// &lt;/summary&gt; public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.RegisterAttached("IsExpanded", typeof(bool), typeof(GridAnimationBehavior), new FrameworkPropertyMetadata(OnIsExpandedChanged)); public static void SetIsExpanded(DependencyObject dependencyObject, bool value) { dependencyObject.SetValue(IsExpandedProperty, value); } #endregion #region Attached Duration DependencyProperty /// &lt;summary&gt; /// Register the "Duration" attached property /// &lt;/summary&gt; public static readonly DependencyProperty DurationProperty = DependencyProperty.RegisterAttached("Duration", typeof(TimeSpan), typeof(GridAnimationBehavior), new FrameworkPropertyMetadata(TimeSpan.FromMilliseconds(200))); public static void SetDuration(DependencyObject dependencyObject, TimeSpan value) { dependencyObject.SetValue(DurationProperty, value); } private static TimeSpan GetDuration(DependencyObject dependencyObject) { return (TimeSpan)dependencyObject.GetValue(DurationProperty); } #endregion #region GridCellSize DependencyProperty /// &lt;summary&gt; /// Use a private "GridCellSize" dependency property as a temporary backing /// store for the last expanded grid cell size (row height or column width). /// &lt;/summary&gt; private static readonly DependencyProperty GridCellSizeProperty = DependencyProperty.Register("GridCellSize", typeof(double), typeof(GridAnimationBehavior), new UIPropertyMetadata(0.0)); private static void SetGridCellSize(DependencyObject dependencyObject, double value) { dependencyObject.SetValue(GridCellSizeProperty, value); } private static double GetGridCellSize(DependencyObject dependencyObject) { return (double)dependencyObject.GetValue(GridCellSizeProperty); } #endregion /// &lt;summary&gt; /// Called when the attached &lt;c&gt;IsExpanded&lt;/c&gt; property changed. /// &lt;/summary&gt; private static void OnIsExpandedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { var duration = GetDuration(dependencyObject); var rowDefinition = dependencyObject as RowDefinition; if (rowDefinition != null) { // The IsExpanded attached property of a RowDefinition changed if ((bool)e.NewValue) { var expandedHeight = GetGridCellSize(rowDefinition); if (expandedHeight &gt; 0) { // Animate row height back to saved expanded height. AnimationHelper.AnimateGridRowExpandCollapse(rowDefinition, true, expandedHeight, rowDefinition.ActualHeight, 0, duration); } } else { // Save expanded height and animate row height down to zero. SetGridCellSize(rowDefinition, rowDefinition.ActualHeight); AnimationHelper.AnimateGridRowExpandCollapse(rowDefinition, false, rowDefinition.ActualHeight, 0, 0, duration); } } var columnDefinition = dependencyObject as ColumnDefinition; if (columnDefinition != null) { // The IsExpanded attached property of a ColumnDefinition changed if ((bool)e.NewValue) { var expandedWidth = GetGridCellSize(columnDefinition); if (expandedWidth &gt; 0) { // Animate column width back to saved expanded width. AnimationHelper.AnimateGridColumnExpandCollapse(columnDefinition, true, expandedWidth, columnDefinition.ActualWidth, 0, duration); } } else { // Save expanded width and animate column width down to zero. SetGridCellSize(columnDefinition, columnDefinition.ActualWidth); AnimationHelper.AnimateGridColumnExpandCollapse(columnDefinition, false, columnDefinition.ActualWidth, 0, 0, duration); } } } } </code></pre> <p>Note that I tweaked Nigel's code a bit to use a parameter of type TimeSpan for the animation duration instead of separate seconds and milliseconds parameters.</p> <p>This behavior makes the animation of grid rows/columns MVVM friendly (XAML-only, no code behind required). Example:</p> <pre><code>&lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*" Behaviors:GridAnimationBehavior.IsExpanded="{Binding IsUpperPaneVisible}" /&gt; &lt;RowDefinition Height="*" /&gt; &lt;RowDefinition Height="*" Behaviors:GridAnimationBehavior.IsExpanded="{Binding IsLowerPaneVisible}" /&gt; &lt;/Grid.RowDefinitions&gt; </code></pre> <p>I added this answer because the original poster asked for a pure XAML solution.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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