RecipeEditorControlViewModel.cs 46 KB

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