RecipeEditorControl.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Data;
  5. using System.Windows.Media;
  6. using System.Xml;
  7. using System.Diagnostics;
  8. using System.Windows.Controls.Primitives;
  9. namespace Aitex.UI.RecipeEditor
  10. {
  11. /// <summary>
  12. /// Interaction logic for RecipeEditorControl.xaml
  13. /// </summary>
  14. public partial class RecipeEditorControl : UserControl
  15. {
  16. public RecipeEditorControl()
  17. {
  18. InitializeComponent();
  19. ControlViewModel = new RecipeEditorControlViewModel() { GridStackPanel = stackPanel1,HeadWrapPanel =headStackpanel};
  20. Loaded += new RoutedEventHandler(RecipeEditorView_Loaded);
  21. }
  22. /// <summary>
  23. /// RecipeEditor's view model
  24. /// </summary>
  25. public RecipeEditorControlViewModel ControlViewModel { get; set; }
  26. private void RecipeEditorView_Loaded(object sender, RoutedEventArgs e)
  27. {
  28. grid1.DataContext = ControlViewModel;
  29. }
  30. /// <summary>
  31. /// update recipe variations
  32. /// </summary>
  33. /// <param name="sender"></param>
  34. /// <param name="e"></param>
  35. private void NewButtonPanelLoaded(object sender, RoutedEventArgs e)
  36. {
  37. try
  38. {
  39. string variation = (string)((Button)sender).Tag;
  40. //newButton.IsOpen = false;
  41. XmlDocument doc = new XmlDocument();
  42. var dir = new System.IO.FileInfo(Process.GetCurrentProcess().MainModule.FileName).Directory;
  43. string xmlPath = dir + "\\Config\\" + variation + ".xml";
  44. doc.Load(xmlPath);
  45. //ControlViewModel.LoadRecipe(doc.SelectSingleNode("/Aitex/TableRecipeFormat").OuterXml, doc.SelectSingleNode("/Aitex/TableRecipeData").OuterXml);
  46. }
  47. catch (Exception ex)
  48. {
  49. System.Diagnostics.Debug.WriteLine(ex.Message);
  50. }
  51. }
  52. }
  53. public class GridOptions
  54. {
  55. //暂时不处理实时显示的事情
  56. #region 显示边框信息
  57. public static readonly DependencyProperty ShowBorderProperty =
  58. DependencyProperty.RegisterAttached("ShowBorder", typeof(bool), typeof(GridOptions)
  59. , new PropertyMetadata(OnShowBorderChanged));
  60. public static bool GetShowBorder(DependencyObject obj)
  61. {
  62. return (bool)obj.GetValue(ShowBorderProperty);
  63. }
  64. //private static bool flag = false;
  65. public static void SetShowBorder(DependencyObject obj, bool value)
  66. {
  67. GridLoaded(obj as Grid, null);
  68. obj.SetValue(ShowBorderProperty, value);
  69. }
  70. public static void OnShowBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  71. {
  72. var grid = d as Grid;
  73. if ((bool)e.OldValue)
  74. grid.Loaded -= (s, arg) => { };
  75. else
  76. {
  77. grid.Loaded += new RoutedEventHandler(GridLoaded);
  78. }
  79. }
  80. #endregion
  81. #region 线宽信息
  82. public static readonly DependencyProperty LineThicknessProperty =
  83. DependencyProperty.RegisterAttached("LineThickness", typeof(double), typeof(GridOptions),
  84. new PropertyMetadata(1d, OnGridLineThicknessChanged));
  85. public static double GetLineThickness(DependencyObject obj)
  86. {
  87. return (double)obj.GetValue(LineThicknessProperty);
  88. }
  89. public static void SetLineThickness(DependencyObject obj, double value)
  90. {
  91. obj.SetValue(LineThicknessProperty, value);
  92. }
  93. public static void OnGridLineThicknessChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  94. {
  95. }
  96. #endregion
  97. public static readonly DependencyProperty LineBrushProperty =
  98. DependencyProperty.RegisterAttached("LineBrush", typeof(Brush), typeof(GridOptions),
  99. new PropertyMetadata(Brushes.Gray, OnGridLineBrushChanged));
  100. #region 线画刷问题
  101. public static Brush GetLineBrush(DependencyObject obj)
  102. {
  103. var brush = (Brush)obj.GetValue(LineBrushProperty);
  104. return brush ?? Brushes.LightGray;
  105. }
  106. public static void SetLineBrush(DependencyObject obj, Brush value)
  107. {
  108. obj.SetValue(LineBrushProperty, value);
  109. }
  110. public static void OnGridLineBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  111. {
  112. }
  113. #endregion
  114. private static void GridLoaded(object sender, RoutedEventArgs e)
  115. {
  116. #region 思路
  117. /*
  118. * 1、覆盖所有单元格都要包围上边框。
  119. * 2、边框线不能存在重复。每个单元格绘制【右下】部分,主体绘制右上部分
  120. */
  121. #endregion
  122. var grid = sender as Grid;
  123. foreach (UIElement item in grid.Children)
  124. {
  125. if (item is Border)
  126. {
  127. return;
  128. }
  129. }
  130. var rowCount = Math.Max(1, grid.RowDefinitions.Count);
  131. var columnCount = Math.Max(1, grid.ColumnDefinitions.Count);
  132. #region 初始化标准数组
  133. int[,] flagArray = new int[rowCount, columnCount];
  134. #endregion
  135. var controls = grid.Children;
  136. var count = controls.Count;
  137. var settingThickness = GetLineThickness(grid);
  138. var borderBrush = GetLineBrush(grid);
  139. for (int i = 0; i < count; i++)
  140. {
  141. var item = controls[i] as FrameworkElement;
  142. var row = Grid.GetRow((item));
  143. var column = Grid.GetColumn(item);
  144. var rowSpan = Grid.GetRowSpan(item);
  145. var columnSpan = Grid.GetColumnSpan(item);
  146. for (int rowTemp = 0; rowTemp < rowSpan; rowTemp++)
  147. {
  148. for (int colTemp = 0; colTemp < columnSpan; colTemp++)
  149. {
  150. flagArray[rowTemp + row, colTemp + column] = 1;
  151. }
  152. }
  153. var border = CreateBorder(row, column, rowSpan, columnSpan, settingThickness);
  154. border.BorderBrush = borderBrush;
  155. grid.Children.RemoveAt(i);
  156. border.Child = item;
  157. grid.Children.Insert(i, border);
  158. }
  159. #region 整理为填充单元格
  160. for (int i = 0; i < rowCount; i++)
  161. {
  162. for (int k = 0; k < columnCount; k++)
  163. {
  164. if (flagArray[i, k] == 0)
  165. {
  166. var border = CreateBorder(i, k, 1, 1, settingThickness);
  167. border.BorderBrush = borderBrush;
  168. grid.Children.Add(border);
  169. }
  170. }
  171. }
  172. #endregion
  173. }
  174. private static Border CreateBorder(int row, int column, int rowSpan, int columnSpan, double thickness)
  175. {
  176. var useThickness = new Thickness(0, 0, thickness, thickness);
  177. if (row == 0)
  178. useThickness.Top = thickness;
  179. if (column == 0)
  180. useThickness.Left = thickness;
  181. var border = new Border()
  182. {
  183. BorderThickness = useThickness,
  184. };
  185. Grid.SetRow(border, row);
  186. Grid.SetColumn(border, column);
  187. Grid.SetRowSpan(border, rowSpan);
  188. Grid.SetColumnSpan(border, columnSpan);
  189. return border;
  190. }
  191. }
  192. public class ItemsControlHelper
  193. {
  194. /// <summary>
  195. /// 绑定 enum 类型所有值给 ItemsSource 赋值
  196. /// 必须绑定 SelectedItem
  197. /// </summary>
  198. public static readonly DependencyProperty EnumValuesToItemsSourceProperty = DependencyProperty.RegisterAttached(
  199. "EnumValuesToItemsSource", typeof(bool), typeof(ItemsControlHelper), new PropertyMetadata(default(bool), OnEnumValuesToItemsSourceChanged));
  200. public static void SetEnumValuesToItemsSource(DependencyObject element, bool value)
  201. {
  202. element.SetValue(EnumValuesToItemsSourceProperty, value);
  203. }
  204. public static bool GetEnumValuesToItemsSource(DependencyObject element)
  205. {
  206. return (bool)element.GetValue(EnumValuesToItemsSourceProperty);
  207. }
  208. private static void OnEnumValuesToItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  209. {
  210. if (d is ItemsControl itemsControl && GetEnumValuesToItemsSource(itemsControl))
  211. {
  212. if (itemsControl.IsLoaded)
  213. {
  214. SetItemsSource(itemsControl);
  215. }
  216. else
  217. {
  218. itemsControl.Loaded += ItemsControl_Loaded;
  219. }
  220. }
  221. }
  222. private static void SetItemsSource(ItemsControl itemsControl)
  223. {
  224. var itemsBindingExpression = BindingOperations.GetBinding(itemsControl, ItemsControl.ItemsSourceProperty);
  225. if (itemsBindingExpression != null)
  226. {
  227. throw new InvalidOperationException("When using ItemsControlHelper.EnumValuesToItemsSource, cannot be used ItemsSource at the same time.");
  228. }
  229. if (itemsControl.Items.Count > 0)
  230. {
  231. throw new InvalidOperationException("When using ItemsControlHelper.EnumValuesToItemsSource, Items Collection must be null");
  232. }
  233. var bindingExpression = BindingOperations.GetBindingExpression(itemsControl, Selector.SelectedItemProperty);
  234. if (bindingExpression == null)
  235. {
  236. throw new InvalidOperationException("ItemsControl must be binding SelectedItem property");
  237. }
  238. var binding = bindingExpression.ParentBinding;
  239. var dataType = bindingExpression.DataItem?.GetType();
  240. var paths = binding.Path.Path.Split('.');
  241. foreach (var path in paths)
  242. {
  243. var propertyInfo = dataType?.GetProperty(path);
  244. if (propertyInfo == null)
  245. {
  246. return;
  247. }
  248. dataType = propertyInfo.PropertyType;
  249. }
  250. if (dataType!=null&&!dataType.IsEnum)
  251. {
  252. var underlyingType = Nullable.GetUnderlyingType(dataType);
  253. if (underlyingType == null)
  254. {
  255. return;
  256. }
  257. dataType = underlyingType;
  258. }
  259. var itemsSourceBinding = new Binding();
  260. itemsSourceBinding.Source = Enum.GetValues(dataType);
  261. itemsSourceBinding.Mode = BindingMode.OneWay;
  262. itemsControl.SetBinding(ItemsControl.ItemsSourceProperty, itemsSourceBinding);
  263. }
  264. private static void ItemsControl_Loaded(object sender, RoutedEventArgs e)
  265. {
  266. var itemsControl = (ItemsControl)sender;
  267. itemsControl.Loaded -= ItemsControl_Loaded;
  268. SetItemsSource(itemsControl);
  269. }
  270. }
  271. }