PageRenderTime 62ms CodeModel.GetById 31ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 1ms

/BookReader/BookReader/Controls/GridSplitterExpander.cs

#
C# | 167 lines | 137 code | 29 blank | 1 comment | 12 complexity | 2e2daf3646dd6371419072917e679e1b MD5 | raw file
  1using System;
  2using System.Windows;
  3using System.Windows.Controls;
  4using System.Windows.Controls.Primitives;
  5using System.Windows.Media;
  6using System.Windows.Data;
  7
  8namespace BookReader.Controls
  9{
 10	public class GridSplitterExpander : ContentControl
 11	{
 12		#region --------------------DEPENDENCY PROPERTIES--------------------
 13
 14		public static readonly DependencyProperty OrientationProperty =
 15			   DependencyProperty.Register("Orientation", typeof(Orientation), typeof(GridSplitterExpander));
 16
 17		public Orientation Orientation
 18		{
 19			get { return (Orientation)GetValue(OrientationProperty); }
 20			set { SetValue(OrientationProperty, value); }
 21		}
 22
 23		public static readonly DependencyProperty TitleProperty =
 24			   DependencyProperty.Register("Title", typeof(string), typeof(GridSplitterExpander));
 25
 26		public string Title
 27		{
 28			get { return (string)GetValue(TitleProperty); }
 29			set { SetValue(TitleProperty, value); }
 30		}
 31
 32		public static readonly DependencyProperty GridIndexProperty =
 33			   DependencyProperty.Register("GridIndex", typeof(int), typeof(GridSplitterExpander));
 34
 35		public int GridIndex
 36		{
 37			get { return (int)GetValue(GridIndexProperty); }
 38			set { SetValue(GridIndexProperty, value); }
 39		}
 40
 41        public bool IsExpanded
 42        {
 43            get { return _btnToggle.IsChecked == true ? true : false; }
 44            set { _btnToggle.IsChecked = value; }
 45        }
 46
 47		private GridLength _CatalogSize;
 48        public GridLength CatalogSize
 49        {
 50            get { return _CatalogSize; }
 51            set
 52            {
 53                _CatalogSize = value;
 54
 55                if (Orientation == Orientation.Vertical)
 56                    this._ParentGrid.ColumnDefinitions[GridIndex].Width = _CatalogSize;
 57                else
 58                    this._ParentGrid.RowDefinitions[GridIndex].Height = _CatalogSize;
 59            }
 60        }
 61		#endregion
 62
 63		private Grid _ParentGrid;
 64		private GridSplitter _gridSplitter;
 65		private ToggleButton _btnToggle;
 66		private TextBlock _TitleBlock;
 67
 68		protected override void OnInitialized(EventArgs e)
 69		{
 70			base.OnInitialized(e);
 71
 72			if( System.ComponentModel.DesignerProperties.GetIsInDesignMode(this) ) return;
 73			//if (DesignHelper.IsInDesignMode()) return;
 74
 75			_ParentGrid = ((Grid)this.Parent);
 76
 77			_btnToggle = new ToggleButton();
 78			_btnToggle.SetValue(Panel.ZIndexProperty, 1);
 79			_btnToggle.Checked += new RoutedEventHandler(OnToggleChecked);
 80			_btnToggle.Unchecked += new RoutedEventHandler(OnToggleUnchecked);
 81
 82			_gridSplitter = new GridSplitter();
 83			_gridSplitter.SetValue(Panel.ZIndexProperty, 0);
 84			_gridSplitter.HorizontalAlignment = HorizontalAlignment.Stretch;
 85			_gridSplitter.VerticalAlignment = VerticalAlignment.Stretch;
 86			_gridSplitter.DragCompleted += new DragCompletedEventHandler(OnDragCompleted);
 87			_gridSplitter.Background = (Brush)FindResource("ToolBarToggleButtonVerticalBackground");
 88
 89			_TitleBlock = new TextBlock();
 90			_TitleBlock.SetValue(Panel.ZIndexProperty, 1);
 91			_TitleBlock.FontWeight = FontWeights.Bold;
 92
 93			Binding bind = new Binding("Title");
 94			bind.Source = this;
 95			_TitleBlock.SetBinding(TextBlock.TextProperty, bind);
 96
 97			if (Orientation == Orientation.Vertical)
 98			{
 99				_btnToggle.Height = _btnToggle.Width;
100				_btnToggle.SetValue(Grid.ColumnProperty, 1);
101
102				_btnToggle.HorizontalAlignment = HorizontalAlignment.Stretch;
103				_btnToggle.VerticalAlignment = VerticalAlignment.Top;
104				_btnToggle.Style = (Style)FindResource("GridSplitterExpanderVerticalButton");
105
106				_gridSplitter.SetValue(Grid.ColumnProperty, 1);
107
108				_TitleBlock.SetValue(Grid.ColumnProperty, 1);
109				_TitleBlock.HorizontalAlignment = HorizontalAlignment.Center;
110				_TitleBlock.VerticalAlignment = VerticalAlignment.Bottom;
111				_TitleBlock.Margin = new Thickness(0, 0, 0, 5);
112				_TitleBlock.LayoutTransform = new RotateTransform(-90, 0, 0);
113			}
114			else
115			{
116				_btnToggle.Width = _btnToggle.Height;
117				_btnToggle.SetValue(Grid.RowProperty, 1);
118
119				_btnToggle.HorizontalAlignment = HorizontalAlignment.Right;
120				_btnToggle.VerticalAlignment = VerticalAlignment.Stretch;
121				_btnToggle.Style = (Style)FindResource("GridSplitterExpanderHorizontalButton");
122
123				_gridSplitter.SetValue(Grid.RowProperty, 1);
124
125				_TitleBlock.SetValue(Grid.RowProperty, 1);
126				_TitleBlock.HorizontalAlignment = HorizontalAlignment.Left;
127				_TitleBlock.VerticalAlignment = VerticalAlignment.Center;
128				_TitleBlock.Margin = new Thickness(5, 0, 0, 0);
129			}
130			_ParentGrid.Children.Add(_btnToggle);
131			_ParentGrid.Children.Add(_gridSplitter);
132			_ParentGrid.Children.Add(_TitleBlock);
133		}
134
135		private void OnToggleChecked(object sender, RoutedEventArgs e)
136		{
137			if (Orientation == Orientation.Vertical)
138			{
139                _CatalogSize = this._ParentGrid.ColumnDefinitions[GridIndex].Width;
140				this._ParentGrid.ColumnDefinitions[GridIndex].Width = new GridLength(0);
141			}
142			else
143			{
144                _CatalogSize = this._ParentGrid.RowDefinitions[GridIndex].Height;
145				this._ParentGrid.RowDefinitions[GridIndex].Height = new GridLength(0);
146			}
147		}
148
149		private void OnToggleUnchecked(object sender, RoutedEventArgs e)
150		{
151			if (Orientation == Orientation.Vertical)
152                this._ParentGrid.ColumnDefinitions[GridIndex].Width = _CatalogSize;
153			else
154                this._ParentGrid.RowDefinitions[GridIndex].Height = _CatalogSize;
155		}
156
157		private void OnDragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
158		{
159			if (Orientation == Orientation.Vertical)
160                _CatalogSize = this._ParentGrid.ColumnDefinitions[GridIndex].Width;
161			else
162                _CatalogSize = this._ParentGrid.RowDefinitions[GridIndex].Height;
163
164			this._btnToggle.IsChecked = false;
165		}
166	}
167}