GridHelper.cs 12 KB

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