PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/BookReader/BookReader/Controls/GridSplitterExpander.cs

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