RecipeEditorControlViewModel.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Runtime.Serialization;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Media;
  14. using Venus_Core;
  15. using Venus_Core.Attributes;
  16. using Venus_Unity;
  17. using Newtonsoft.Json;
  18. namespace Aitex.UI.RecipeEditor
  19. {
  20. public class RecipeEditorControlViewModel : INotifyPropertyChanged
  21. {
  22. //当前选中的配方
  23. private Recipe m_currentRecipe;
  24. public Recipe CurrentRecipe
  25. {
  26. get { return m_currentRecipe; }
  27. set { m_currentRecipe = value; InvokePropertyChanged("CurrentRecipe"); }
  28. }
  29. public StackPanel GridStackPanel { get; set; } //step容器
  30. List<SolidColorBrush> solidColorBrushes = new List<SolidColorBrush>();//step模块颜色
  31. public WrapPanel HeadWrapPanel { get; set; } //head容器
  32. Grid currentGrid;
  33. private int copyIndex=-1;
  34. public RecipeEditorControlViewModel()
  35. {
  36. solidColorBrushes.Add(new SolidColorBrush(Colors.Coral));
  37. solidColorBrushes.Add(new SolidColorBrush(Colors.Cyan));
  38. solidColorBrushes.Add(new SolidColorBrush(Colors.Honeydew));
  39. }
  40. public void OnAddStep(int index)
  41. {
  42. if (CurrentRecipe != null)
  43. {
  44. RecipeStep recipeStep;
  45. if (copyIndex == -1)
  46. {
  47. recipeStep = new RecipeStep();
  48. recipeStep.LstUnit.Add(new PressureByPressureModeUnit());
  49. recipeStep.LstUnit.Add(new TCPUnit());
  50. recipeStep.LstUnit.Add(new BiasUnit());
  51. recipeStep.LstUnit.Add(new GasControlUnit());
  52. recipeStep.LstUnit.Add(new ESCHVUnit());
  53. recipeStep.LstUnit.Add(new ProcessKitUnit());
  54. CurrentRecipe.Steps.Insert(index-1,recipeStep);
  55. }
  56. else
  57. {
  58. var t= JsonConvert.SerializeObject(CurrentRecipe);
  59. recipeStep= Recipe.Load(t).Steps[copyIndex - 1];
  60. CurrentRecipe.Steps.Insert(index-1, recipeStep);
  61. }
  62. RecipeStepToGridColumn(recipeStep, currentGrid, index,true );
  63. for (int i = 1; i <= CurrentRecipe.Steps.Count; i++)
  64. {
  65. CurrentRecipe.Steps[i - 1].StepNo = i;
  66. }
  67. }
  68. }
  69. public void OnDeleteStep(int index)
  70. {
  71. if (CurrentRecipe != null && CurrentRecipe.Steps.Count>1)
  72. {
  73. CurrentRecipe.Steps.RemoveAt(index);
  74. currentGrid.Children.RemoveRange(currentGrid.RowDefinitions.Count * (index+1), currentGrid.RowDefinitions.Count);
  75. for (int i = 1; i <= CurrentRecipe.Steps.Count; i++)
  76. {
  77. CurrentRecipe.Steps[i - 1].StepNo = i;
  78. }
  79. }
  80. }
  81. //int index = 0;
  82. /// <summary>
  83. /// 选中recipe,显示界面
  84. /// </summary>
  85. /// <param name="recipe"></param>
  86. public void LoadRecipe(Recipe recipe)
  87. {
  88. copyIndex = -1;
  89. GridStackPanel.Children.Clear();
  90. CurrentRecipe = null;
  91. GC.Collect(); // This should pick up the control removed at a previous MouseDown
  92. GC.WaitForPendingFinalizers(); // Doesn't help either
  93. GC.Collect();
  94. GC.WaitForPendingFinalizers(); // Doesn't help either
  95. CurrentRecipe = recipe;
  96. Grid grid = new Grid();
  97. grid.Margin = new Thickness(15);
  98. //index = 0;
  99. for (int i = 0; i < recipe.Steps.Count; i++)
  100. {
  101. RecipeStepToGridColumn(recipe.Steps[i], grid,i,false);
  102. }
  103. GridStackPanel.Children.Add(grid);
  104. LoadHeadWrapPanel(recipe);
  105. currentGrid = grid;
  106. }
  107. /// <summary>
  108. /// 选中recipe,头部界面显示
  109. /// </summary>
  110. /// <param name="recipe"></param>
  111. private void LoadHeadWrapPanel(Recipe recipe)
  112. {
  113. HeadWrapPanel.Children.Clear();
  114. Type type = recipe.Header.GetType();
  115. foreach (var propertyInfo in type.GetProperties())
  116. {
  117. TextBlock textBlock = new TextBlock();
  118. textBlock.FontSize = 15;
  119. //textBlock.Width = 100;
  120. textBlock.Text = propertyInfo.Name + ":";
  121. textBlock.Margin = new Thickness(15, 0, 0, 0);
  122. textBlock.VerticalAlignment = VerticalAlignment.Center;
  123. HeadWrapPanel.Children.Add(textBlock);
  124. Binding binding = new Binding()
  125. {
  126. Source = recipe.Header, // 数据源
  127. Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  128. Mode = BindingMode.TwoWay, // 绑定模式
  129. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  130. };
  131. var propertyTypeName = propertyInfo.PropertyType.Name;
  132. var propertyInfoName=propertyInfo.Name;
  133. Control control = new Control();
  134. switch (propertyTypeName)
  135. {
  136. case "Int32":
  137. case "String":
  138. control = new TextBox();
  139. control.Margin = new Thickness(1, 0, 0, 0);
  140. control.BorderThickness = new Thickness(0,0,0,1);
  141. //control.BorderBrush = Brushes.Black;
  142. control.FontSize = 15;
  143. control.Foreground = Brushes.Green;
  144. control.Background = Brushes.Transparent;
  145. control.VerticalAlignment = VerticalAlignment.Center;
  146. control.MinWidth = 15;
  147. control.SetBinding(TextBox.TextProperty, binding);
  148. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  149. if (objAttrs.Length > 0)
  150. {
  151. (control as TextBox).IsReadOnly = true;
  152. }
  153. break;
  154. case "Boolean":
  155. control = new CheckBox();
  156. control.SetBinding(CheckBox.IsCheckedProperty, binding);
  157. break;
  158. default:
  159. control = new ComboBox();
  160. control.Height = 23;
  161. control.SetBinding(ComboBox.SelectedItemProperty, binding);
  162. ItemsControlHelper.SetEnumValuesToItemsSource(control, true);
  163. break;
  164. }
  165. HeadWrapPanel.Children.Add(control);
  166. }
  167. }
  168. private void RecipeStepToGridColumn(RecipeStep recipeStep, Grid grid,int index,bool isInsert)
  169. {
  170. int location = 1;
  171. if (isInsert == true)
  172. {
  173. location -= 1;
  174. }
  175. Type recipeType = recipeStep.GetType();
  176. int cycleCount = 1;
  177. if (index == 0)
  178. {
  179. cycleCount = 2;
  180. }
  181. for (int j = 0; j< cycleCount; j++)
  182. {
  183. int i = 0;
  184. ColumnDefinition col1 = new ColumnDefinition();
  185. //col1.MinWidth = 50;
  186. grid.ColumnDefinitions.Insert(index,col1);
  187. Binding stepcheckbinding = null;
  188. //grid.MinWidth = 200;
  189. foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
  190. {
  191. string propertyInfoName = propertyInfo.Name;
  192. string propertyTypeName = propertyInfo.PropertyType.Name;
  193. if (propertyInfoName != "LstUnit")
  194. {
  195. if (index == 0 && grid.ColumnDefinitions.Count == 1)
  196. {
  197. RowDefinition row1 = new RowDefinition();
  198. grid.RowDefinitions.Add(row1);
  199. }
  200. if (grid.ColumnDefinitions.Count == 1 && j == 0)
  201. {
  202. TextBox textBlock = new TextBox();
  203. textBlock.IsReadOnly = true;
  204. textBlock.Text = propertyInfoName;
  205. grid.Children.Add(textBlock);
  206. textBlock.Background = new SolidColorBrush(Colors.BurlyWood);
  207. Grid.SetRow(textBlock, i);
  208. Grid.SetColumn(textBlock, 0);
  209. }
  210. else
  211. {
  212. Binding binding = new Binding()
  213. {
  214. Source = recipeStep, // 数据源
  215. Path = new PropertyPath(propertyInfoName), // 需绑定的数据源属性名
  216. Mode = BindingMode.TwoWay, // 绑定模式
  217. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  218. };
  219. switch (propertyTypeName)
  220. {
  221. case "Int32":
  222. case "String":
  223. TextBox textBox = new TextBox();
  224. if (stepcheckbinding == null)
  225. {
  226. textBox.IsEnabled = true;
  227. }
  228. else
  229. {
  230. textBox.SetBinding(TextBox.IsEnabledProperty, stepcheckbinding);
  231. }
  232. textBox.SetBinding(TextBox.TextProperty, binding);
  233. grid.Children.Add(textBox);
  234. Grid.SetRow(textBox, i);
  235. Grid.SetColumn(textBox, index + location);
  236. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  237. if (objAttrs.Length > 0)
  238. {
  239. textBox.IsReadOnly = true;
  240. ContextMenu contextmenu = new ContextMenu();
  241. textBox.ContextMenu = contextmenu;
  242. MenuItem menuItemDelete = new MenuItem();
  243. menuItemDelete.Header = "删除";
  244. menuItemDelete.Click += MenuItemDelete_Click;
  245. MenuItem menuItemLeftInsert = new MenuItem();
  246. menuItemLeftInsert.Header = "左插入";
  247. menuItemLeftInsert.Click += MenuItemLeftInsert_Click;
  248. MenuItem menuItemRightInsert = new MenuItem();
  249. menuItemRightInsert.Header = "右插入";
  250. menuItemRightInsert.Click += MenuItemRightInsert_Click;
  251. MenuItem menuItemCopy = new MenuItem();
  252. menuItemCopy.Tag = textBox.Text;
  253. menuItemCopy.Header = "复制";
  254. menuItemCopy.Click += MenuItemCopy_Click;
  255. contextmenu.Items.Add(menuItemCopy);
  256. contextmenu.Items.Add(menuItemDelete);
  257. contextmenu.Items.Add(menuItemLeftInsert);
  258. contextmenu.Items.Add(menuItemRightInsert);
  259. }
  260. break;
  261. case "Boolean":
  262. CheckBox checkBox = new CheckBox();
  263. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  264. grid.Children.Add(checkBox);
  265. Grid.SetRow(checkBox, i);
  266. Grid.SetColumn(checkBox, index + location);
  267. if (stepcheckbinding == null)
  268. {
  269. stepcheckbinding = new Binding
  270. {
  271. Source = checkBox, // 数据源
  272. Path = new PropertyPath("IsChecked"), // 需绑定的数据源属性名
  273. Mode = BindingMode.TwoWay, // 绑定模式
  274. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  275. };
  276. }
  277. else
  278. {
  279. if (stepcheckbinding == null)
  280. {
  281. checkBox.IsEnabled = true;
  282. }
  283. else
  284. {
  285. checkBox.SetBinding(TextBox.IsEnabledProperty, stepcheckbinding);
  286. }
  287. }
  288. break;
  289. default:
  290. ComboBox comboBox = new ComboBox();
  291. comboBox.Style = new Style();
  292. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  293. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  294. grid.Children.Add(comboBox);
  295. Grid.SetRow(comboBox, i);
  296. Grid.SetColumn(comboBox, index + location);
  297. if (stepcheckbinding == null)
  298. {
  299. comboBox.IsEnabled = true;
  300. }
  301. else
  302. {
  303. comboBox.SetBinding(TextBox.IsEnabledProperty, stepcheckbinding);
  304. }
  305. break;
  306. }
  307. }
  308. i++;
  309. }
  310. }
  311. int k = 0;
  312. recipeStep.LstUnit.ToList().ForEach(x =>
  313. {
  314. Type unitType = x.GetType();
  315. Binding checkbinding = null;
  316. foreach (PropertyInfo propertyInfo in unitType.GetProperties())
  317. {
  318. if (index == 0 && grid.ColumnDefinitions.Count == 1)
  319. {
  320. RowDefinition row1 = new RowDefinition();
  321. grid.RowDefinitions.Add(row1);
  322. }
  323. if (grid.ColumnDefinitions.Count == 1 && j == 0)
  324. {
  325. TextBox textBlock = new TextBox();
  326. textBlock.IsReadOnly = true;
  327. textBlock.Text = propertyInfo.Name;
  328. textBlock.Background = solidColorBrushes[k % 3];
  329. grid.Children.Add(textBlock);
  330. Grid.SetRow(textBlock, i);
  331. Grid.SetColumn(textBlock, 0);
  332. }
  333. else
  334. {
  335. Binding binding = new Binding()
  336. {
  337. Source = recipeStep.LstUnit[k], // 数据源
  338. //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  339. Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  340. Mode = BindingMode.TwoWay, // 绑定模式
  341. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  342. };
  343. var item = propertyInfo.PropertyType.Name;
  344. if (propertyInfo.Name == "UnitName")
  345. {
  346. TextBox textBlock1 = new TextBox();
  347. textBlock1.Text = propertyInfo.GetValue(x).ToString();
  348. textBlock1.IsReadOnly = true;
  349. grid.Children.Add(textBlock1);
  350. Grid.SetRow(textBlock1, i);
  351. Grid.SetColumn(textBlock1, index + location);
  352. }
  353. else
  354. {
  355. switch (item)
  356. {
  357. case "Int32":
  358. case "String":
  359. TextBox textBox = new TextBox();
  360. if (checkbinding == null)
  361. {
  362. textBox.IsEnabled = true;
  363. }
  364. else
  365. {
  366. textBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
  367. }
  368. textBox.SetBinding(TextBox.TextProperty, binding);
  369. grid.Children.Add(textBox);
  370. Grid.SetRow(textBox, i);
  371. Grid.SetColumn(textBox, index + location);
  372. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  373. if (objAttrs.Length > 0)
  374. {
  375. textBox.IsReadOnly = true;
  376. }
  377. break;
  378. case "Boolean":
  379. CheckBox checkBox = new CheckBox();
  380. if (checkbinding == null)
  381. {
  382. checkbinding = new Binding
  383. {
  384. Source = checkBox, // 数据源
  385. Path = new PropertyPath("IsChecked"), // 需绑定的数据源属性名
  386. Mode = BindingMode.TwoWay, // 绑定模式
  387. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  388. };
  389. }
  390. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  391. grid.Children.Add(checkBox);
  392. Grid.SetRow(checkBox, i);
  393. Grid.SetColumn(checkBox, index+location);
  394. break;
  395. default:
  396. ComboBox comboBox = new ComboBox();
  397. if (checkbinding == null)
  398. {
  399. comboBox.IsEnabled = true;
  400. }
  401. else
  402. {
  403. comboBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
  404. }
  405. comboBox.Style = new Style();
  406. comboBox.Background = Brushes.White;
  407. string path = propertyInfo.Name;
  408. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  409. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  410. grid.Children.Add(comboBox);
  411. Grid.SetRow(comboBox, i);
  412. Grid.SetColumn(comboBox, index + location);
  413. break;
  414. }
  415. }
  416. }
  417. i++;
  418. }
  419. k++;
  420. });
  421. //index++;
  422. }
  423. }
  424. //private void RecipeStepToGridColumn(RecipeStep recipeStep, Grid grid)
  425. //{
  426. // try
  427. // {
  428. // Type recipeType = recipeStep.GetType();
  429. // int i = 0;
  430. // if (grid.ColumnDefinitions.Count == 0)
  431. // {
  432. // ColumnDefinition col1 = new ColumnDefinition();//Key
  433. // grid.ColumnDefinitions.Add(col1);
  434. // }
  435. // ColumnDefinition col2 = new ColumnDefinition();//Value
  436. // grid.ColumnDefinitions.Add(col2);
  437. // foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
  438. // {
  439. // string propertyInfoName = propertyInfo.Name;
  440. // string propertyTypeName = propertyInfo.PropertyType.Name;
  441. // if (propertyInfoName != "LstUnit")
  442. // {
  443. // if (index == 0)
  444. // {
  445. // RowDefinition row1 = new RowDefinition();
  446. // grid.RowDefinitions.Add(row1);
  447. // }
  448. // Binding binding = new Binding()
  449. // {
  450. // Source = recipeStep, // 数据源
  451. // Path = new PropertyPath(propertyInfoName), // 需绑定的数据源属性名
  452. // Mode = BindingMode.TwoWay, // 绑定模式
  453. // UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  454. // };
  455. // switch (propertyTypeName)
  456. // {
  457. // case "Int32":
  458. // case "String":
  459. // TextBox textBox = new TextBox();
  460. // textBox.SetBinding(TextBox.TextProperty, binding);
  461. // grid.Children.Add(textBox);
  462. // Grid.SetRow(textBox, i);
  463. // Grid.SetColumn(textBox, grid.ColumnDefinitions.Count - 1);
  464. // object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  465. // if (objAttrs.Length > 0)
  466. // {
  467. // textBox.IsReadOnly = true;
  468. // //textBox.SetBinding(TextBox.TextProperty, currentGrid.ColumnDefinitions.c);
  469. // ContextMenu contextmenu = new ContextMenu();
  470. // textBox.ContextMenu = contextmenu;
  471. // MenuItem menuItemDelete = new MenuItem();
  472. // menuItemDelete.Header = "删除";
  473. // menuItemDelete.Click += MenuItemDelete_Click;
  474. // MenuItem menuItemInsert = new MenuItem();
  475. // menuItemInsert.Header = "左插入";
  476. // menuItemInsert.Click += MenuItemInsert_Click;
  477. // MenuItem menuItemCopy = new MenuItem();
  478. // menuItemCopy.Tag = textBox.Text;
  479. // menuItemCopy.Header = "复制";
  480. // menuItemCopy.Click += MenuItemCopy_Click;
  481. // contextmenu.Items.Add(menuItemCopy);
  482. // contextmenu.Items.Add(menuItemDelete);
  483. // contextmenu.Items.Add(menuItemInsert);
  484. // }
  485. // break;
  486. // case "Boolean":
  487. // CheckBox checkBox = new CheckBox();
  488. // checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  489. // grid.Children.Add(checkBox);
  490. // Grid.SetRow(checkBox, i);
  491. // Grid.SetColumn(checkBox, grid.ColumnDefinitions.Count - 1);
  492. // break;
  493. // default:
  494. // ComboBox comboBox = new ComboBox();
  495. // comboBox.Style = new Style();
  496. // comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  497. // ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  498. // grid.Children.Add(comboBox);
  499. // Grid.SetRow(comboBox, i);
  500. // Grid.SetColumn(comboBox, grid.ColumnDefinitions.Count - 1);
  501. // break;
  502. // }
  503. // if (grid.ColumnDefinitions.Count == 2)
  504. // {
  505. // TextBox textBlock = new TextBox();
  506. // textBlock.IsReadOnly = true;
  507. // textBlock.Text = propertyInfoName;
  508. // grid.Children.Add(textBlock);
  509. // textBlock.Background = new SolidColorBrush(Colors.BurlyWood);
  510. // Grid.SetRow(textBlock, i);
  511. // Grid.SetColumn(textBlock, 0);
  512. // }
  513. // i++;
  514. // }
  515. // }
  516. // List<int> ints = new List<int>();
  517. // ints.Add(recipeType.GetProperties().Length);
  518. // int k = 0;
  519. // recipeStep.LstUnit.ToList().ForEach(x =>
  520. // {
  521. // Type unitType = x.GetType();
  522. // Binding checkbinding = null;
  523. // foreach (PropertyInfo propertyInfo in unitType.GetProperties())
  524. // {
  525. // Binding binding = new Binding()
  526. // {
  527. // Source = recipeStep.LstUnit[k], // 数据源
  528. // //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  529. // Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  530. // Mode = BindingMode.TwoWay, // 绑定模式
  531. // UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  532. // };
  533. // if (index == 0)
  534. // {
  535. // RowDefinition row1 = new RowDefinition();
  536. // grid.RowDefinitions.Add(row1);
  537. // }
  538. // var item = propertyInfo.PropertyType.Name;
  539. // if (propertyInfo.Name == "UnitName")
  540. // {
  541. // TextBox textBlock1 = new TextBox();
  542. // //textBlock1.SetBinding(TextBlock.TextProperty, binding);
  543. // textBlock1.Text = propertyInfo.GetValue(x).ToString();
  544. // //textBlock1.BorderThickness = new Thickness(0);
  545. // //textBlock1.Foreground = Brushes.Red;
  546. // textBlock1.IsReadOnly = true;
  547. // //textBlock1.HorizontalAlignment = HorizontalAlignment.Center;
  548. // grid.Children.Add(textBlock1);
  549. // Grid.SetRow(textBlock1, i);
  550. // Grid.SetColumn(textBlock1, grid.ColumnDefinitions.Count - 1);
  551. // }
  552. // else
  553. // {
  554. // //CheckBox check = new CheckBox();
  555. // //check.IsChecked = false;
  556. // switch (item)
  557. // {
  558. // case "Int32":
  559. // case "String":
  560. // TextBox textBox = new TextBox();
  561. // //textBox.SetBinding(TextBox.IsEnabledProperty, check.IsChecked);
  562. // //textBox.IsEnabled= true;
  563. // if (checkbinding == null)
  564. // {
  565. // textBox.IsEnabled = true;
  566. // }
  567. // else
  568. // {
  569. // textBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
  570. // }
  571. // //textBox.BorderBrush = Brushes.Transparent;
  572. // //textBox.BorderThickness = new Thickness(0);
  573. // textBox.SetBinding(TextBox.TextProperty, binding);
  574. // grid.Children.Add(textBox);
  575. // Grid.SetRow(textBox, i);
  576. // Grid.SetColumn(textBox, grid.ColumnDefinitions.Count - 1);
  577. // object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  578. // if (objAttrs.Length > 0)
  579. // {
  580. // textBox.IsReadOnly = true;
  581. // }
  582. // break;
  583. // case "Boolean":
  584. // CheckBox checkBox = new CheckBox();
  585. // checkbinding = new Binding
  586. // {
  587. // Source = checkBox, // 数据源
  588. // //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  589. // Path = new PropertyPath("IsChecked"), // 需绑定的数据源属性名
  590. // Mode = BindingMode.TwoWay, // 绑定模式
  591. // UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  592. // };
  593. // //checkbinding = binding;
  594. // //checkBox.Margin = new Thickness(1);
  595. // checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  596. // grid.Children.Add(checkBox);
  597. // Grid.SetRow(checkBox, i);
  598. // Grid.SetColumn(checkBox, grid.ColumnDefinitions.Count - 1);
  599. // break;
  600. // default:
  601. // ComboBox comboBox = new ComboBox();
  602. // if (checkbinding == null)
  603. // {
  604. // comboBox.IsEnabled = true;
  605. // }
  606. // else
  607. // {
  608. // comboBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
  609. // }
  610. // comboBox.Style = new Style();
  611. // comboBox.Background = Brushes.White;
  612. // string path = propertyInfo.Name;
  613. // comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  614. // ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  615. // grid.Children.Add(comboBox);
  616. // Grid.SetRow(comboBox, i);
  617. // Grid.SetColumn(comboBox, grid.ColumnDefinitions.Count - 1);
  618. // break;
  619. // }
  620. // }
  621. // if (grid.ColumnDefinitions.Count == 2)
  622. // {
  623. // TextBox textBlock = new TextBox();
  624. // textBlock.IsReadOnly = true;
  625. // textBlock.Text = propertyInfo.Name;
  626. // textBlock.Background = solidColorBrushes[k % 3];
  627. // grid.Children.Add(textBlock);
  628. // Grid.SetRow(textBlock, i);
  629. // Grid.SetColumn(textBlock, 0);
  630. // }
  631. // i++;
  632. // }
  633. // ints.Add(unitType.GetProperties().Length);
  634. // k++;
  635. // });
  636. // index++;
  637. // }
  638. // catch
  639. // { }
  640. // //return new ValueTuple<Grid, List<int>>(grid, ints);
  641. //}
  642. /// <summary>
  643. /// step转换为Grid
  644. /// </summary>
  645. /// <param name="recipeStep"></param>
  646. /// <returns></returns>
  647. private ValueTuple<Grid, List<int>> RecipeStepToGrid(RecipeStep recipeStep)
  648. {
  649. Grid grid = new Grid();
  650. ContextMenu contextmenu = new ContextMenu();
  651. contextmenu.Tag = grid;
  652. grid.ContextMenu= contextmenu;
  653. MenuItem menuItemDelete = new MenuItem();
  654. menuItemDelete.Header = "删除";
  655. menuItemDelete.Click += MenuItemDelete_Click;
  656. MenuItem menuItemInsert = new MenuItem();
  657. menuItemInsert.Header = "左插入";
  658. menuItemInsert.Click += MenuItemLeftInsert_Click;
  659. contextmenu.Items.Add(menuItemDelete);
  660. contextmenu.Items.Add(menuItemInsert);
  661. List<int> ints = new List<int>();
  662. ColumnDefinition col1 = new ColumnDefinition();//Key
  663. ColumnDefinition col2 = new ColumnDefinition();//Value
  664. grid.ColumnDefinitions.Add(col1);
  665. grid.ColumnDefinitions.Add(col2);
  666. GridOptions.SetShowBorder(grid, true);
  667. GridOptions.SetLineBrush(grid, Brushes.White);
  668. GridOptions.SetLineThickness(grid, 1);
  669. grid.Margin = new Thickness(15, 5, 0, 0);
  670. Type recipeType = recipeStep.GetType();
  671. int i = 0;
  672. foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
  673. {
  674. string propertyInfoName = propertyInfo.Name;
  675. string propertyTypeName = propertyInfo.PropertyType.Name;
  676. if (propertyInfoName != "LstUnit")
  677. {
  678. RowDefinition row1 = new RowDefinition();
  679. grid.RowDefinitions.Add(row1);
  680. Binding binding = new Binding()
  681. {
  682. Source = recipeStep, // 数据源
  683. Path = new PropertyPath(propertyInfoName), // 需绑定的数据源属性名
  684. Mode = BindingMode.TwoWay, // 绑定模式
  685. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  686. };
  687. switch (propertyTypeName)
  688. {
  689. case "Int32":
  690. case "String":
  691. TextBox textBox = new TextBox();
  692. textBox.SetBinding(TextBox.TextProperty, binding);
  693. grid.Children.Add(textBox);
  694. Grid.SetRow(textBox, i);
  695. Grid.SetColumn(textBox, 1);
  696. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  697. if (objAttrs.Length > 0)
  698. {
  699. textBox.IsReadOnly = true;
  700. }
  701. break;
  702. case "Boolean":
  703. CheckBox checkBox = new CheckBox();
  704. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  705. grid.Children.Add(checkBox);
  706. Grid.SetRow(checkBox, i);
  707. Grid.SetColumn(checkBox, 1);
  708. break;
  709. default:
  710. ComboBox comboBox = new ComboBox();
  711. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  712. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  713. grid.Children.Add(comboBox);
  714. Grid.SetRow(comboBox, i);
  715. Grid.SetColumn(comboBox, 1);
  716. if (propertyInfo.Name == "PressureUnitMode")
  717. {
  718. comboBox.SelectionChanged += ComboBox_SelectionChanged;
  719. }
  720. break;
  721. }
  722. TextBlock textBlock = new TextBlock();
  723. textBlock.Text = propertyInfoName;
  724. grid.Children.Add(textBlock);
  725. Grid.SetRow(textBlock, i);
  726. Grid.SetColumn(textBlock, 0);
  727. i++;
  728. }
  729. }
  730. ints.Add(recipeType.GetProperties().Length);
  731. int k = 0;
  732. recipeStep.LstUnit.ToList().ForEach(x =>
  733. {
  734. Type unitType = x.GetType();
  735. foreach (PropertyInfo propertyInfo in unitType.GetProperties())
  736. {
  737. Binding binding = new Binding()
  738. {
  739. Source = recipeStep.LstUnit[k], // 数据源
  740. //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  741. Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  742. Mode = BindingMode.TwoWay, // 绑定模式
  743. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  744. };
  745. RowDefinition row1 = new RowDefinition();
  746. grid.RowDefinitions.Add(row1);
  747. var item = propertyInfo.PropertyType.Name;
  748. if (propertyInfo.Name == "UnitName")
  749. {
  750. TextBlock textBlock1 = new TextBlock();
  751. //textBlock1.SetBinding(TextBlock.TextProperty, binding);
  752. textBlock1.Text = propertyInfo.GetValue(x).ToString();
  753. textBlock1.Foreground = Brushes.Red;
  754. //textBlock1.HorizontalAlignment = HorizontalAlignment.Center;
  755. grid.Children.Add(textBlock1);
  756. Grid.SetRow(textBlock1, i);
  757. Grid.SetColumn(textBlock1, 1);
  758. }
  759. else
  760. {
  761. switch (item)
  762. {
  763. case "Int32":
  764. case "String":
  765. TextBox textBox = new TextBox();
  766. textBox.BorderBrush = Brushes.Transparent;
  767. textBox.SetBinding(TextBox.TextProperty, binding);
  768. grid.Children.Add(textBox);
  769. Grid.SetRow(textBox, i);
  770. Grid.SetColumn(textBox, 1);
  771. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  772. if (objAttrs.Length > 0)
  773. {
  774. textBox.IsReadOnly = true;
  775. }
  776. break;
  777. case "Boolean":
  778. CheckBox checkBox = new CheckBox();
  779. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  780. grid.Children.Add(checkBox);
  781. Grid.SetRow(checkBox, i);
  782. Grid.SetColumn(checkBox, 1);
  783. break;
  784. default:
  785. ComboBox comboBox = new ComboBox();
  786. comboBox.BorderBrush = Brushes.Transparent;
  787. //comboBox.Background = Brushes.White;
  788. string path = propertyInfo.Name;
  789. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  790. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  791. grid.Children.Add(comboBox);
  792. Grid.SetRow(comboBox, i);
  793. Grid.SetColumn(comboBox, 1);
  794. break;
  795. }
  796. }
  797. TextBlock textBlock = new TextBlock();
  798. textBlock.Text = propertyInfo.Name;
  799. grid.Children.Add(textBlock);
  800. Grid.SetRow(textBlock, i);
  801. Grid.SetColumn(textBlock, 0);
  802. i++;
  803. }
  804. ints.Add(unitType.GetProperties().Length);
  805. k++;
  806. });
  807. return new ValueTuple<Grid, List<int>>(grid, ints);
  808. }
  809. private void MenuItemLeftInsert_Click(object sender, RoutedEventArgs e)
  810. {
  811. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  812. int insertIndex = Convert.ToInt32(t.Text);
  813. OnAddStep(insertIndex);
  814. }
  815. private void MenuItemRightInsert_Click(object sender, RoutedEventArgs e)
  816. {
  817. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  818. int insertIndex = Convert.ToInt32(t.Text);
  819. OnAddStep(insertIndex+1);
  820. }
  821. private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
  822. {
  823. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  824. int deleteIndex = Convert.ToInt32(t.Text);
  825. OnDeleteStep(deleteIndex - 1);
  826. }
  827. private void MenuItemCopy_Click(object sender, RoutedEventArgs e)
  828. {
  829. var t= ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  830. copyIndex =Convert.ToInt32( t.Text);
  831. }
  832. /// <summary>
  833. /// 获取父控件
  834. /// </summary>
  835. /// <typeparam name="T"></typeparam>
  836. /// <param name="obj"></param>
  837. /// <param name="name"></param>
  838. /// <returns></returns>
  839. public T GetParentObject2<T>(DependencyObject obj, string name) where T : FrameworkElement
  840. {
  841. DependencyObject parent = VisualTreeHelper.GetParent(obj);
  842. while (parent != null)
  843. {
  844. if (parent is T && (((T)parent).Name == name || string.IsNullOrEmpty(name)))
  845. {
  846. return (T)parent;
  847. }
  848. parent = VisualTreeHelper.GetParent(parent);
  849. }
  850. return null;
  851. }
  852. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  853. {
  854. if (e.RemovedItems.Count > 0)
  855. {
  856. ComboBox comboBox = sender as ComboBox;
  857. Grid grid = GetParentObject<Grid>(comboBox, "");
  858. int index= GridStackPanel.Children.IndexOf(grid);
  859. RecipeStep recipeStep = CurrentRecipe.Steps[index];
  860. recipeStep.LstUnit.RemoveAt(0);
  861. if (comboBox.SelectedIndex == 0)
  862. {
  863. recipeStep.LstUnit.Insert(0, new PressureByPressureModeUnit());
  864. }
  865. else
  866. {
  867. recipeStep.LstUnit.Insert(0, new PressureByValveModeUnit());
  868. }
  869. GridStackPanel.Children.Remove(grid);
  870. GridStackPanel.Children.Insert(index, RecipeStepToGrid(recipeStep).Item1);
  871. }
  872. }
  873. public T GetParentObject<T>(DependencyObject obj, string name) where T : FrameworkElement
  874. {
  875. DependencyObject parent = VisualTreeHelper.GetParent(obj);
  876. while (parent != null)
  877. {
  878. if (parent is T && (((T)parent).Name == name || string.IsNullOrEmpty(name)))
  879. {
  880. return (T)parent;
  881. }
  882. parent = VisualTreeHelper.GetParent(parent);
  883. }
  884. return null;
  885. }
  886. private Tuple<DataGrid,List<int>> RecipeStepToDataGrid(RecipeStep recipeStep)
  887. {
  888. DataGrid dataGrid = new DataGrid();
  889. List<int> ints = new List<int>();
  890. DataTable dataTable = new DataTable();
  891. dataTable.Columns.Add("Key");
  892. dataTable.Columns.Add("Value");
  893. dataGrid.CanUserAddRows = false;
  894. dataGrid.AutoGenerateColumns = true;
  895. dataGrid.SelectionUnit = DataGridSelectionUnit.Cell;
  896. dataGrid.SetValue(System.Windows.Controls.VirtualizingStackPanel.IsVirtualizingProperty, false);
  897. Type recipeType = recipeStep.GetType();
  898. foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
  899. {
  900. if (propertyInfo.Name != "LstUnit")
  901. {
  902. dataTable.Rows.Add(propertyInfo.Name, propertyInfo.GetValue(recipeStep));
  903. }
  904. }
  905. ints.Add(dataTable.Rows.Count);
  906. recipeStep.LstUnit.ToList().ForEach(k =>
  907. {
  908. //List<dynamic> dynamics = new List<dynamic>();
  909. Type type = k.GetType();
  910. ints.Add(type.GetProperties().Length);
  911. foreach (PropertyInfo propertyInfo in type.GetProperties())
  912. {
  913. var name = propertyInfo.Name;
  914. var value = propertyInfo.GetValue(k);
  915. //dynamicModel.PropertyName = name;
  916. //dynamicModel.Property = value;
  917. dataTable.Rows.Add(name, value);
  918. }
  919. });
  920. dataGrid.ItemsSource = dataTable.DefaultView;
  921. //});
  922. return new Tuple<DataGrid, List<int>>(dataGrid, ints);
  923. }
  924. private void ChangeDataGridRowColor(DataGrid dataGrid,List<int> ints)
  925. {
  926. int count = 0;
  927. for (int i = 0; i < ints.Count; i++)
  928. {
  929. for (int j = 0; j < ints[i]; j++)
  930. {
  931. dataGrid.UpdateLayout();
  932. int index = count + j;
  933. DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items[index]) as DataGridRow;
  934. row.Background = solidColorBrushes[i];
  935. }
  936. count += ints[i];
  937. }
  938. }
  939. private void ChangeGridRowColor(Grid grid, List<int> ints)
  940. {
  941. int count = 0;
  942. for (int i = 0; i < ints.Count; i++)
  943. {
  944. for (int j = 0; j < ints[i]; j++)
  945. {
  946. //dataGrid.UpdateLayout();
  947. int index = count + j;
  948. //DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items[index]) as DataGridRow;
  949. //grid.RowDefinitions[index]
  950. }
  951. count += ints[i];
  952. }
  953. }
  954. #region INotifyPropertyChanged
  955. public event PropertyChangedEventHandler PropertyChanged;
  956. public void InvokePropertyChanged(string propertyName)
  957. {
  958. if (PropertyChanged != null)
  959. {
  960. PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
  961. }
  962. }
  963. #endregion INotifyPropertyChanged
  964. }
  965. }