andrew4582

HidableGridSplitter

Sep 4th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4.  
  5. namespace System.Windows.Controls
  6. {
  7.     /// <summary>
  8.     /// Grid splitter that show or hides the following row when the visibility of the splitter is changed.
  9.     /// </summary>
  10.     public class HidableGridSplitter : GridSplitter
  11.     {
  12.         GridLength height;
  13.  
  14.         public HidableGridSplitter()
  15.         {
  16.             this.IsVisibleChanged += HideableGridSplitter_IsVisibleChanged;
  17.             this.Initialized += HideableGridSplitter_Initialized;
  18.         }
  19.  
  20.         void HideableGridSplitter_Initialized(object sender, EventArgs e)
  21.         {
  22.             //Cache the initial RowDefinition height,
  23.             //so it is not always assumed to be "Auto"
  24.             Grid parent = base.Parent as Grid;
  25.             if (parent == null)
  26.                 return;
  27.             int rowIndex = Grid.GetRow(this);
  28.             if (rowIndex + 1 >= parent.RowDefinitions.Count)
  29.                 return;
  30.             var lastRow = parent.RowDefinitions[rowIndex + 1];
  31.             height = lastRow.Height;
  32.         }
  33.  
  34.         void HideableGridSplitter_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  35.         {
  36.             Grid parent = base.Parent as Grid;
  37.             if (parent == null)
  38.                 return;
  39.  
  40.             int rowIndex = Grid.GetRow(this);
  41.  
  42.             if (rowIndex + 1 >= parent.RowDefinitions.Count)
  43.                 return;
  44.  
  45.             var lastRow = parent.RowDefinitions[rowIndex + 1];
  46.  
  47.             if (this.Visibility == Visibility.Visible)
  48.             {
  49.                 lastRow.Height = height;
  50.             }
  51.             else
  52.             {
  53.                 height = lastRow.Height;
  54.                 lastRow.Height = new GridLength(0);
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment