GridHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Media;
  4. using System.Windows.Shapes;
  5. namespace MECF.Framework.UI.Core.Style
  6. {
  7. public class GridHelper
  8. {
  9. public static Brush GetColumn0(DependencyObject obj)
  10. {
  11. return (Brush)obj.GetValue(Column0Property);
  12. }
  13. public static void SetColumn0(DependencyObject obj, Brush value)
  14. {
  15. obj.SetValue(Column0Property, value);
  16. }
  17. // Using a DependencyProperty as the backing store for Column0. This enables animation, styling, binding, etc...
  18. public static readonly DependencyProperty Column0Property =
  19. DependencyProperty.RegisterAttached("Column0", typeof(Brush), typeof(GridHelper), new FrameworkPropertyMetadata(null, PropertyChangedCallback));
  20. public static Brush GetColumn1(DependencyObject obj)
  21. {
  22. return (Brush)obj.GetValue(Column1Property);
  23. }
  24. public static void SetColumn1(DependencyObject obj, Brush value)
  25. {
  26. obj.SetValue(Column1Property, value);
  27. }
  28. // Using a DependencyProperty as the backing store for Column1. This enables animation, styling, binding, etc...
  29. public static readonly DependencyProperty Column1Property =
  30. DependencyProperty.RegisterAttached("Column1", typeof(Brush), typeof(GridHelper), new FrameworkPropertyMetadata(null, PropertyChangedCallback));
  31. public static Brush GetColumn2(DependencyObject obj)
  32. {
  33. return (Brush)obj.GetValue(Column2Property);
  34. }
  35. public static void SetColumn2(DependencyObject obj, Brush value)
  36. {
  37. obj.SetValue(Column2Property, value);
  38. }
  39. // Using a DependencyProperty as the backing store for Column2. This enables animation, styling, binding, etc...
  40. public static readonly DependencyProperty Column2Property =
  41. DependencyProperty.RegisterAttached("Column2", typeof(Brush), typeof(GridHelper), new FrameworkPropertyMetadata(null, PropertyChangedCallback));
  42. public static Brush GetColumn3(DependencyObject obj)
  43. {
  44. return (Brush)obj.GetValue(Column3Property);
  45. }
  46. public static void SetColumn3(DependencyObject obj, Brush value)
  47. {
  48. obj.SetValue(Column3Property, value);
  49. }
  50. // Using a DependencyProperty as the backing store for Column3. This enables animation, styling, binding, etc...
  51. public static readonly DependencyProperty Column3Property =
  52. DependencyProperty.RegisterAttached("Column3", typeof(Brush), typeof(GridHelper), new FrameworkPropertyMetadata(null, PropertyChangedCallback));
  53. public static Brush GetStroke(DependencyObject obj)
  54. {
  55. return (Brush)obj.GetValue(StrokeProperty);
  56. }
  57. public static void SetStroke(DependencyObject obj, Brush value)
  58. {
  59. obj.SetValue(StrokeProperty, value);
  60. }
  61. // Using a DependencyProperty as the backing store for Stroke. This enables animation, styling, binding, etc...
  62. public static readonly DependencyProperty StrokeProperty =
  63. DependencyProperty.RegisterAttached("Stroke", typeof(Brush), typeof(GridHelper), new PropertyMetadata(Brushes.White));
  64. public static Brush GetColumn4(DependencyObject obj)
  65. {
  66. return (Brush)obj.GetValue(Column4Property);
  67. }
  68. public static void SetColumn4(DependencyObject obj, Brush value)
  69. {
  70. obj.SetValue(Column4Property, value);
  71. }
  72. // Using a DependencyProperty as the backing store for Column4. This enables animation, styling, binding, etc...
  73. public static readonly DependencyProperty Column4Property =
  74. DependencyProperty.RegisterAttached("Column4", typeof(Brush), typeof(GridHelper), new PropertyMetadata(null, PropertyChangedCallback));
  75. public static Brush GetColumn5(DependencyObject obj)
  76. {
  77. return (Brush)obj.GetValue(Column5Property);
  78. }
  79. public static void SetColumn5(DependencyObject obj, Brush value)
  80. {
  81. obj.SetValue(Column5Property, value);
  82. }
  83. // Using a DependencyProperty as the backing store for Column5. This enables animation, styling, binding, etc...
  84. public static readonly DependencyProperty Column5Property =
  85. DependencyProperty.RegisterAttached("Column5", typeof(Brush), typeof(GridHelper), new PropertyMetadata(null, PropertyChangedCallback));
  86. public static Brush GetColumn6(DependencyObject obj)
  87. {
  88. return (Brush)obj.GetValue(Column6Property);
  89. }
  90. public static void SetColumn6(DependencyObject obj, Brush value)
  91. {
  92. obj.SetValue(Column6Property, value);
  93. }
  94. // Using a DependencyProperty as the backing store for Column6. This enables animation, styling, binding, etc...
  95. public static readonly DependencyProperty Column6Property =
  96. DependencyProperty.RegisterAttached("Column6", typeof(Brush), typeof(GridHelper), new PropertyMetadata(null, PropertyChangedCallback));
  97. public static Brush GetColumn7(DependencyObject obj)
  98. {
  99. return (Brush)obj.GetValue(Column7Property);
  100. }
  101. public static void SetColumn7(DependencyObject obj, Brush value)
  102. {
  103. obj.SetValue(Column7Property, value);
  104. }
  105. // Using a DependencyProperty as the backing store for Column7. This enables animation, styling, binding, etc...
  106. public static readonly DependencyProperty Column7Property =
  107. DependencyProperty.RegisterAttached("Column7", typeof(Brush), typeof(GridHelper), new PropertyMetadata(null, PropertyChangedCallback));
  108. public static Brush GetColumn8(DependencyObject obj)
  109. {
  110. return (Brush)obj.GetValue(Column8Property);
  111. }
  112. public static void SetColumn8(DependencyObject obj, Brush value)
  113. {
  114. obj.SetValue(Column8Property, value);
  115. }
  116. // Using a DependencyProperty as the backing store for Column8. This enables animation, styling, binding, etc...
  117. public static readonly DependencyProperty Column8Property =
  118. DependencyProperty.RegisterAttached("Column8", typeof(Brush), typeof(GridHelper), new PropertyMetadata(null, PropertyChangedCallback));
  119. public static Brush GetColumn9(DependencyObject obj)
  120. {
  121. return (Brush)obj.GetValue(Column9Property);
  122. }
  123. public static void SetColumn9(DependencyObject obj, Brush value)
  124. {
  125. obj.SetValue(Column9Property, value);
  126. }
  127. // Using a DependencyProperty as the backing store for Column9. This enables animation, styling, binding, etc...
  128. public static readonly DependencyProperty Column9Property =
  129. DependencyProperty.RegisterAttached("Column9", typeof(Brush), typeof(GridHelper), new PropertyMetadata(null, PropertyChangedCallback));
  130. public static Brush GetColumn10(DependencyObject obj)
  131. {
  132. return (Brush)obj.GetValue(Column10Property);
  133. }
  134. public static void SetColumn10(DependencyObject obj, Brush value)
  135. {
  136. obj.SetValue(Column10Property, value);
  137. }
  138. // Using a DependencyProperty as the backing store for Column10. This enables animation, styling, binding, etc...
  139. public static readonly DependencyProperty Column10Property =
  140. DependencyProperty.RegisterAttached("Column10", typeof(Brush), typeof(GridHelper), new PropertyMetadata(null, PropertyChangedCallback));
  141. public static Brush GetColumn11(DependencyObject obj)
  142. {
  143. return (Brush)obj.GetValue(Column11Property);
  144. }
  145. public static void SetColumn11(DependencyObject obj, Brush value)
  146. {
  147. obj.SetValue(Column11Property, value);
  148. }
  149. // Using a DependencyProperty as the backing store for Column11. This enables animation, styling, binding, etc...
  150. public static readonly DependencyProperty Column11Property =
  151. DependencyProperty.RegisterAttached("Column11", typeof(Brush), typeof(GridHelper), new PropertyMetadata(null, PropertyChangedCallback));
  152. public static Brush GetColumn12(DependencyObject obj)
  153. {
  154. return (Brush)obj.GetValue(Column12Property);
  155. }
  156. public static void SetColumn12(DependencyObject obj, Brush value)
  157. {
  158. obj.SetValue(Column12Property, value);
  159. }
  160. // Using a DependencyProperty as the backing store for Column12. This enables animation, styling, binding, etc...
  161. public static readonly DependencyProperty Column12Property =
  162. DependencyProperty.RegisterAttached("Column12", typeof(Brush), typeof(GridHelper), new PropertyMetadata(null, PropertyChangedCallback));
  163. public static Brush GetColumn13(DependencyObject obj)
  164. {
  165. return (Brush)obj.GetValue(Column13Property);
  166. }
  167. public static void SetColumn13(DependencyObject obj, Brush value)
  168. {
  169. obj.SetValue(Column13Property, value);
  170. }
  171. // Using a DependencyProperty as the backing store for Column13. This enables animation, styling, binding, etc...
  172. public static readonly DependencyProperty Column13Property =
  173. DependencyProperty.RegisterAttached("Column13", typeof(Brush), typeof(GridHelper), new PropertyMetadata(null, PropertyChangedCallback));
  174. public static Brush GetColumn14(DependencyObject obj)
  175. {
  176. return (Brush)obj.GetValue(Column14Property);
  177. }
  178. public static void SetColumn14(DependencyObject obj, Brush value)
  179. {
  180. obj.SetValue(Column14Property, value);
  181. }
  182. // Using a DependencyProperty as the backing store for Column14. This enables animation, styling, binding, etc...
  183. public static readonly DependencyProperty Column14Property =
  184. DependencyProperty.RegisterAttached("Column14", typeof(Brush), typeof(GridHelper), new PropertyMetadata(null, PropertyChangedCallback));
  185. public static Brush GetColumn15(DependencyObject obj)
  186. {
  187. return (Brush)obj.GetValue(Column15Property);
  188. }
  189. public static void SetColumn15(DependencyObject obj, Brush value)
  190. {
  191. obj.SetValue(Column15Property, value);
  192. }
  193. // Using a DependencyProperty as the backing store for Column15. This enables animation, styling, binding, etc...
  194. public static readonly DependencyProperty Column15Property =
  195. DependencyProperty.RegisterAttached("Column15", typeof(Brush), typeof(GridHelper), new PropertyMetadata(null, PropertyChangedCallback));
  196. private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  197. {
  198. var grid = d as Grid;
  199. if (grid != null)
  200. {
  201. var col = int.Parse(e.Property.Name.Substring("Column".Length));
  202. RoutedEventHandler setup = null;
  203. setup = (s, x) =>
  204. {
  205. grid.Loaded -= setup;
  206. var count = VisualTreeHelper.GetChildrenCount(grid);
  207. for (int i = 0; i < count; i++)
  208. {
  209. var control = grid.Children[i];
  210. var columnSpan = Grid.GetColumnSpan(control);
  211. if (columnSpan > 1)
  212. {
  213. continue;
  214. }
  215. var column = Grid.GetColumn(control);
  216. if (col == column)
  217. {
  218. if (control is Label && e.NewValue == Brushes.Transparent)
  219. {
  220. //((Label)control).Style = (System.Windows.Style)Application.Current.TryFindResource("MiddleCenterLabel");
  221. }
  222. var rect = new Rectangle();
  223. rect.StrokeThickness = 1;
  224. rect.Stroke = GetStroke(grid);
  225. rect.Fill = e.NewValue as Brush;
  226. var row = Grid.GetRow(control);
  227. Grid.SetRow(rect, row);
  228. Grid.SetColumn(rect, col);
  229. grid.Children.Insert(i, rect);
  230. i++;
  231. count++;
  232. }
  233. }
  234. };
  235. grid.Loaded += setup;
  236. }
  237. }
  238. }
  239. }