RecipeEditorControlViewModel.cs 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. //grid.MinWidth = 200;
  188. foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
  189. {
  190. string propertyInfoName = propertyInfo.Name;
  191. string propertyTypeName = propertyInfo.PropertyType.Name;
  192. if (propertyInfoName != "LstUnit")
  193. {
  194. if (index == 0 && grid.ColumnDefinitions.Count == 1)
  195. {
  196. RowDefinition row1 = new RowDefinition();
  197. grid.RowDefinitions.Add(row1);
  198. }
  199. if (grid.ColumnDefinitions.Count == 1 && j == 0)
  200. {
  201. TextBox textBlock = new TextBox();
  202. textBlock.IsReadOnly = true;
  203. textBlock.Text = propertyInfoName;
  204. grid.Children.Add(textBlock);
  205. textBlock.Background = new SolidColorBrush(Colors.BurlyWood);
  206. Grid.SetRow(textBlock, i);
  207. Grid.SetColumn(textBlock, 0);
  208. }
  209. else
  210. {
  211. Binding binding = new Binding()
  212. {
  213. Source = recipeStep, // 数据源
  214. Path = new PropertyPath(propertyInfoName), // 需绑定的数据源属性名
  215. Mode = BindingMode.TwoWay, // 绑定模式
  216. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  217. };
  218. switch (propertyTypeName)
  219. {
  220. case "Int32":
  221. case "String":
  222. TextBox textBox = new TextBox();
  223. textBox.SetBinding(TextBox.TextProperty, binding);
  224. grid.Children.Add(textBox);
  225. Grid.SetRow(textBox, i);
  226. Grid.SetColumn(textBox, index + location);
  227. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  228. if (objAttrs.Length > 0)
  229. {
  230. textBox.IsReadOnly = true;
  231. ContextMenu contextmenu = new ContextMenu();
  232. textBox.ContextMenu = contextmenu;
  233. MenuItem menuItemDelete = new MenuItem();
  234. menuItemDelete.Header = "删除";
  235. menuItemDelete.Click += MenuItemDelete_Click;
  236. MenuItem menuItemLeftInsert = new MenuItem();
  237. menuItemLeftInsert.Header = "左插入";
  238. menuItemLeftInsert.Click += MenuItemLeftInsert_Click;
  239. MenuItem menuItemRightInsert = new MenuItem();
  240. menuItemRightInsert.Header = "右插入";
  241. menuItemRightInsert.Click += MenuItemRightInsert_Click;
  242. MenuItem menuItemCopy = new MenuItem();
  243. menuItemCopy.Tag = textBox.Text;
  244. menuItemCopy.Header = "复制";
  245. menuItemCopy.Click += MenuItemCopy_Click;
  246. contextmenu.Items.Add(menuItemCopy);
  247. contextmenu.Items.Add(menuItemDelete);
  248. contextmenu.Items.Add(menuItemLeftInsert);
  249. contextmenu.Items.Add(menuItemRightInsert);
  250. }
  251. break;
  252. case "Boolean":
  253. CheckBox checkBox = new CheckBox();
  254. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  255. grid.Children.Add(checkBox);
  256. Grid.SetRow(checkBox, i);
  257. Grid.SetColumn(checkBox, index + location);
  258. break;
  259. default:
  260. ComboBox comboBox = new ComboBox();
  261. comboBox.Style = new Style();
  262. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  263. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  264. grid.Children.Add(comboBox);
  265. Grid.SetRow(comboBox, i);
  266. Grid.SetColumn(comboBox, index + location);
  267. break;
  268. }
  269. }
  270. i++;
  271. }
  272. }
  273. int k = 0;
  274. recipeStep.LstUnit.ToList().ForEach(x =>
  275. {
  276. Type unitType = x.GetType();
  277. Binding checkbinding = null;
  278. foreach (PropertyInfo propertyInfo in unitType.GetProperties())
  279. {
  280. if (index == 0 && grid.ColumnDefinitions.Count == 1)
  281. {
  282. RowDefinition row1 = new RowDefinition();
  283. grid.RowDefinitions.Add(row1);
  284. }
  285. if (grid.ColumnDefinitions.Count == 1 && j == 0)
  286. {
  287. TextBox textBlock = new TextBox();
  288. textBlock.IsReadOnly = true;
  289. textBlock.Text = propertyInfo.Name;
  290. textBlock.Background = solidColorBrushes[k % 3];
  291. grid.Children.Add(textBlock);
  292. Grid.SetRow(textBlock, i);
  293. Grid.SetColumn(textBlock, 0);
  294. }
  295. else
  296. {
  297. Binding binding = new Binding()
  298. {
  299. Source = recipeStep.LstUnit[k], // 数据源
  300. //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  301. Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  302. Mode = BindingMode.TwoWay, // 绑定模式
  303. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  304. };
  305. var item = propertyInfo.PropertyType.Name;
  306. if (propertyInfo.Name == "UnitName")
  307. {
  308. TextBox textBlock1 = new TextBox();
  309. textBlock1.Text = propertyInfo.GetValue(x).ToString();
  310. textBlock1.IsReadOnly = true;
  311. grid.Children.Add(textBlock1);
  312. Grid.SetRow(textBlock1, i);
  313. Grid.SetColumn(textBlock1, index + location);
  314. }
  315. else
  316. {
  317. switch (item)
  318. {
  319. case "Int32":
  320. case "String":
  321. TextBox textBox = new TextBox();
  322. if (checkbinding == null)
  323. {
  324. textBox.IsEnabled = true;
  325. }
  326. else
  327. {
  328. textBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
  329. }
  330. textBox.SetBinding(TextBox.TextProperty, binding);
  331. grid.Children.Add(textBox);
  332. Grid.SetRow(textBox, i);
  333. Grid.SetColumn(textBox, index + location);
  334. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  335. if (objAttrs.Length > 0)
  336. {
  337. textBox.IsReadOnly = true;
  338. }
  339. break;
  340. case "Boolean":
  341. CheckBox checkBox = new CheckBox();
  342. checkbinding = new Binding
  343. {
  344. Source = checkBox, // 数据源
  345. Path = new PropertyPath("IsChecked"), // 需绑定的数据源属性名
  346. Mode = BindingMode.TwoWay, // 绑定模式
  347. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  348. };
  349. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  350. grid.Children.Add(checkBox);
  351. Grid.SetRow(checkBox, i);
  352. Grid.SetColumn(checkBox, index+location);
  353. break;
  354. default:
  355. ComboBox comboBox = new ComboBox();
  356. if (checkbinding == null)
  357. {
  358. comboBox.IsEnabled = true;
  359. }
  360. else
  361. {
  362. comboBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
  363. }
  364. comboBox.Style = new Style();
  365. comboBox.Background = Brushes.White;
  366. string path = propertyInfo.Name;
  367. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  368. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  369. grid.Children.Add(comboBox);
  370. Grid.SetRow(comboBox, i);
  371. Grid.SetColumn(comboBox, index + location);
  372. break;
  373. }
  374. }
  375. }
  376. i++;
  377. }
  378. k++;
  379. });
  380. //index++;
  381. }
  382. }
  383. //private void RecipeStepToGridColumn(RecipeStep recipeStep, Grid grid)
  384. //{
  385. // try
  386. // {
  387. // Type recipeType = recipeStep.GetType();
  388. // int i = 0;
  389. // if (grid.ColumnDefinitions.Count == 0)
  390. // {
  391. // ColumnDefinition col1 = new ColumnDefinition();//Key
  392. // grid.ColumnDefinitions.Add(col1);
  393. // }
  394. // ColumnDefinition col2 = new ColumnDefinition();//Value
  395. // grid.ColumnDefinitions.Add(col2);
  396. // foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
  397. // {
  398. // string propertyInfoName = propertyInfo.Name;
  399. // string propertyTypeName = propertyInfo.PropertyType.Name;
  400. // if (propertyInfoName != "LstUnit")
  401. // {
  402. // if (index == 0)
  403. // {
  404. // RowDefinition row1 = new RowDefinition();
  405. // grid.RowDefinitions.Add(row1);
  406. // }
  407. // Binding binding = new Binding()
  408. // {
  409. // Source = recipeStep, // 数据源
  410. // Path = new PropertyPath(propertyInfoName), // 需绑定的数据源属性名
  411. // Mode = BindingMode.TwoWay, // 绑定模式
  412. // UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  413. // };
  414. // switch (propertyTypeName)
  415. // {
  416. // case "Int32":
  417. // case "String":
  418. // TextBox textBox = new TextBox();
  419. // textBox.SetBinding(TextBox.TextProperty, binding);
  420. // grid.Children.Add(textBox);
  421. // Grid.SetRow(textBox, i);
  422. // Grid.SetColumn(textBox, grid.ColumnDefinitions.Count - 1);
  423. // object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  424. // if (objAttrs.Length > 0)
  425. // {
  426. // textBox.IsReadOnly = true;
  427. // //textBox.SetBinding(TextBox.TextProperty, currentGrid.ColumnDefinitions.c);
  428. // ContextMenu contextmenu = new ContextMenu();
  429. // textBox.ContextMenu = contextmenu;
  430. // MenuItem menuItemDelete = new MenuItem();
  431. // menuItemDelete.Header = "删除";
  432. // menuItemDelete.Click += MenuItemDelete_Click;
  433. // MenuItem menuItemInsert = new MenuItem();
  434. // menuItemInsert.Header = "左插入";
  435. // menuItemInsert.Click += MenuItemInsert_Click;
  436. // MenuItem menuItemCopy = new MenuItem();
  437. // menuItemCopy.Tag = textBox.Text;
  438. // menuItemCopy.Header = "复制";
  439. // menuItemCopy.Click += MenuItemCopy_Click;
  440. // contextmenu.Items.Add(menuItemCopy);
  441. // contextmenu.Items.Add(menuItemDelete);
  442. // contextmenu.Items.Add(menuItemInsert);
  443. // }
  444. // break;
  445. // case "Boolean":
  446. // CheckBox checkBox = new CheckBox();
  447. // checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  448. // grid.Children.Add(checkBox);
  449. // Grid.SetRow(checkBox, i);
  450. // Grid.SetColumn(checkBox, grid.ColumnDefinitions.Count - 1);
  451. // break;
  452. // default:
  453. // ComboBox comboBox = new ComboBox();
  454. // comboBox.Style = new Style();
  455. // comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  456. // ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  457. // grid.Children.Add(comboBox);
  458. // Grid.SetRow(comboBox, i);
  459. // Grid.SetColumn(comboBox, grid.ColumnDefinitions.Count - 1);
  460. // break;
  461. // }
  462. // if (grid.ColumnDefinitions.Count == 2)
  463. // {
  464. // TextBox textBlock = new TextBox();
  465. // textBlock.IsReadOnly = true;
  466. // textBlock.Text = propertyInfoName;
  467. // grid.Children.Add(textBlock);
  468. // textBlock.Background = new SolidColorBrush(Colors.BurlyWood);
  469. // Grid.SetRow(textBlock, i);
  470. // Grid.SetColumn(textBlock, 0);
  471. // }
  472. // i++;
  473. // }
  474. // }
  475. // List<int> ints = new List<int>();
  476. // ints.Add(recipeType.GetProperties().Length);
  477. // int k = 0;
  478. // recipeStep.LstUnit.ToList().ForEach(x =>
  479. // {
  480. // Type unitType = x.GetType();
  481. // Binding checkbinding = null;
  482. // foreach (PropertyInfo propertyInfo in unitType.GetProperties())
  483. // {
  484. // Binding binding = new Binding()
  485. // {
  486. // Source = recipeStep.LstUnit[k], // 数据源
  487. // //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  488. // Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  489. // Mode = BindingMode.TwoWay, // 绑定模式
  490. // UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  491. // };
  492. // if (index == 0)
  493. // {
  494. // RowDefinition row1 = new RowDefinition();
  495. // grid.RowDefinitions.Add(row1);
  496. // }
  497. // var item = propertyInfo.PropertyType.Name;
  498. // if (propertyInfo.Name == "UnitName")
  499. // {
  500. // TextBox textBlock1 = new TextBox();
  501. // //textBlock1.SetBinding(TextBlock.TextProperty, binding);
  502. // textBlock1.Text = propertyInfo.GetValue(x).ToString();
  503. // //textBlock1.BorderThickness = new Thickness(0);
  504. // //textBlock1.Foreground = Brushes.Red;
  505. // textBlock1.IsReadOnly = true;
  506. // //textBlock1.HorizontalAlignment = HorizontalAlignment.Center;
  507. // grid.Children.Add(textBlock1);
  508. // Grid.SetRow(textBlock1, i);
  509. // Grid.SetColumn(textBlock1, grid.ColumnDefinitions.Count - 1);
  510. // }
  511. // else
  512. // {
  513. // //CheckBox check = new CheckBox();
  514. // //check.IsChecked = false;
  515. // switch (item)
  516. // {
  517. // case "Int32":
  518. // case "String":
  519. // TextBox textBox = new TextBox();
  520. // //textBox.SetBinding(TextBox.IsEnabledProperty, check.IsChecked);
  521. // //textBox.IsEnabled= true;
  522. // if (checkbinding == null)
  523. // {
  524. // textBox.IsEnabled = true;
  525. // }
  526. // else
  527. // {
  528. // textBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
  529. // }
  530. // //textBox.BorderBrush = Brushes.Transparent;
  531. // //textBox.BorderThickness = new Thickness(0);
  532. // textBox.SetBinding(TextBox.TextProperty, binding);
  533. // grid.Children.Add(textBox);
  534. // Grid.SetRow(textBox, i);
  535. // Grid.SetColumn(textBox, grid.ColumnDefinitions.Count - 1);
  536. // object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  537. // if (objAttrs.Length > 0)
  538. // {
  539. // textBox.IsReadOnly = true;
  540. // }
  541. // break;
  542. // case "Boolean":
  543. // CheckBox checkBox = new CheckBox();
  544. // checkbinding = new Binding
  545. // {
  546. // Source = checkBox, // 数据源
  547. // //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  548. // Path = new PropertyPath("IsChecked"), // 需绑定的数据源属性名
  549. // Mode = BindingMode.TwoWay, // 绑定模式
  550. // UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  551. // };
  552. // //checkbinding = binding;
  553. // //checkBox.Margin = new Thickness(1);
  554. // checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  555. // grid.Children.Add(checkBox);
  556. // Grid.SetRow(checkBox, i);
  557. // Grid.SetColumn(checkBox, grid.ColumnDefinitions.Count - 1);
  558. // break;
  559. // default:
  560. // ComboBox comboBox = new ComboBox();
  561. // if (checkbinding == null)
  562. // {
  563. // comboBox.IsEnabled = true;
  564. // }
  565. // else
  566. // {
  567. // comboBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
  568. // }
  569. // comboBox.Style = new Style();
  570. // comboBox.Background = Brushes.White;
  571. // string path = propertyInfo.Name;
  572. // comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  573. // ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  574. // grid.Children.Add(comboBox);
  575. // Grid.SetRow(comboBox, i);
  576. // Grid.SetColumn(comboBox, grid.ColumnDefinitions.Count - 1);
  577. // break;
  578. // }
  579. // }
  580. // if (grid.ColumnDefinitions.Count == 2)
  581. // {
  582. // TextBox textBlock = new TextBox();
  583. // textBlock.IsReadOnly = true;
  584. // textBlock.Text = propertyInfo.Name;
  585. // textBlock.Background = solidColorBrushes[k % 3];
  586. // grid.Children.Add(textBlock);
  587. // Grid.SetRow(textBlock, i);
  588. // Grid.SetColumn(textBlock, 0);
  589. // }
  590. // i++;
  591. // }
  592. // ints.Add(unitType.GetProperties().Length);
  593. // k++;
  594. // });
  595. // index++;
  596. // }
  597. // catch
  598. // { }
  599. // //return new ValueTuple<Grid, List<int>>(grid, ints);
  600. //}
  601. /// <summary>
  602. /// step转换为Grid
  603. /// </summary>
  604. /// <param name="recipeStep"></param>
  605. /// <returns></returns>
  606. private ValueTuple<Grid, List<int>> RecipeStepToGrid(RecipeStep recipeStep)
  607. {
  608. Grid grid = new Grid();
  609. ContextMenu contextmenu = new ContextMenu();
  610. contextmenu.Tag = grid;
  611. grid.ContextMenu= contextmenu;
  612. MenuItem menuItemDelete = new MenuItem();
  613. menuItemDelete.Header = "删除";
  614. menuItemDelete.Click += MenuItemDelete_Click;
  615. MenuItem menuItemInsert = new MenuItem();
  616. menuItemInsert.Header = "左插入";
  617. menuItemInsert.Click += MenuItemLeftInsert_Click;
  618. contextmenu.Items.Add(menuItemDelete);
  619. contextmenu.Items.Add(menuItemInsert);
  620. List<int> ints = new List<int>();
  621. ColumnDefinition col1 = new ColumnDefinition();//Key
  622. ColumnDefinition col2 = new ColumnDefinition();//Value
  623. grid.ColumnDefinitions.Add(col1);
  624. grid.ColumnDefinitions.Add(col2);
  625. GridOptions.SetShowBorder(grid, true);
  626. GridOptions.SetLineBrush(grid, Brushes.White);
  627. GridOptions.SetLineThickness(grid, 1);
  628. grid.Margin = new Thickness(15, 5, 0, 0);
  629. Type recipeType = recipeStep.GetType();
  630. int i = 0;
  631. foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
  632. {
  633. string propertyInfoName = propertyInfo.Name;
  634. string propertyTypeName = propertyInfo.PropertyType.Name;
  635. if (propertyInfoName != "LstUnit")
  636. {
  637. RowDefinition row1 = new RowDefinition();
  638. grid.RowDefinitions.Add(row1);
  639. Binding binding = new Binding()
  640. {
  641. Source = recipeStep, // 数据源
  642. Path = new PropertyPath(propertyInfoName), // 需绑定的数据源属性名
  643. Mode = BindingMode.TwoWay, // 绑定模式
  644. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  645. };
  646. switch (propertyTypeName)
  647. {
  648. case "Int32":
  649. case "String":
  650. TextBox textBox = new TextBox();
  651. textBox.SetBinding(TextBox.TextProperty, binding);
  652. grid.Children.Add(textBox);
  653. Grid.SetRow(textBox, i);
  654. Grid.SetColumn(textBox, 1);
  655. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  656. if (objAttrs.Length > 0)
  657. {
  658. textBox.IsReadOnly = true;
  659. }
  660. break;
  661. case "Boolean":
  662. CheckBox checkBox = new CheckBox();
  663. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  664. grid.Children.Add(checkBox);
  665. Grid.SetRow(checkBox, i);
  666. Grid.SetColumn(checkBox, 1);
  667. break;
  668. default:
  669. ComboBox comboBox = new ComboBox();
  670. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  671. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  672. grid.Children.Add(comboBox);
  673. Grid.SetRow(comboBox, i);
  674. Grid.SetColumn(comboBox, 1);
  675. if (propertyInfo.Name == "PressureUnitMode")
  676. {
  677. comboBox.SelectionChanged += ComboBox_SelectionChanged;
  678. }
  679. break;
  680. }
  681. TextBlock textBlock = new TextBlock();
  682. textBlock.Text = propertyInfoName;
  683. grid.Children.Add(textBlock);
  684. Grid.SetRow(textBlock, i);
  685. Grid.SetColumn(textBlock, 0);
  686. i++;
  687. }
  688. }
  689. ints.Add(recipeType.GetProperties().Length);
  690. int k = 0;
  691. recipeStep.LstUnit.ToList().ForEach(x =>
  692. {
  693. Type unitType = x.GetType();
  694. foreach (PropertyInfo propertyInfo in unitType.GetProperties())
  695. {
  696. Binding binding = new Binding()
  697. {
  698. Source = recipeStep.LstUnit[k], // 数据源
  699. //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  700. Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  701. Mode = BindingMode.TwoWay, // 绑定模式
  702. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  703. };
  704. RowDefinition row1 = new RowDefinition();
  705. grid.RowDefinitions.Add(row1);
  706. var item = propertyInfo.PropertyType.Name;
  707. if (propertyInfo.Name == "UnitName")
  708. {
  709. TextBlock textBlock1 = new TextBlock();
  710. //textBlock1.SetBinding(TextBlock.TextProperty, binding);
  711. textBlock1.Text = propertyInfo.GetValue(x).ToString();
  712. textBlock1.Foreground = Brushes.Red;
  713. //textBlock1.HorizontalAlignment = HorizontalAlignment.Center;
  714. grid.Children.Add(textBlock1);
  715. Grid.SetRow(textBlock1, i);
  716. Grid.SetColumn(textBlock1, 1);
  717. }
  718. else
  719. {
  720. switch (item)
  721. {
  722. case "Int32":
  723. case "String":
  724. TextBox textBox = new TextBox();
  725. textBox.BorderBrush = Brushes.Transparent;
  726. textBox.SetBinding(TextBox.TextProperty, binding);
  727. grid.Children.Add(textBox);
  728. Grid.SetRow(textBox, i);
  729. Grid.SetColumn(textBox, 1);
  730. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  731. if (objAttrs.Length > 0)
  732. {
  733. textBox.IsReadOnly = true;
  734. }
  735. break;
  736. case "Boolean":
  737. CheckBox checkBox = new CheckBox();
  738. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  739. grid.Children.Add(checkBox);
  740. Grid.SetRow(checkBox, i);
  741. Grid.SetColumn(checkBox, 1);
  742. break;
  743. default:
  744. ComboBox comboBox = new ComboBox();
  745. comboBox.BorderBrush = Brushes.Transparent;
  746. //comboBox.Background = Brushes.White;
  747. string path = propertyInfo.Name;
  748. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  749. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  750. grid.Children.Add(comboBox);
  751. Grid.SetRow(comboBox, i);
  752. Grid.SetColumn(comboBox, 1);
  753. break;
  754. }
  755. }
  756. TextBlock textBlock = new TextBlock();
  757. textBlock.Text = propertyInfo.Name;
  758. grid.Children.Add(textBlock);
  759. Grid.SetRow(textBlock, i);
  760. Grid.SetColumn(textBlock, 0);
  761. i++;
  762. }
  763. ints.Add(unitType.GetProperties().Length);
  764. k++;
  765. });
  766. return new ValueTuple<Grid, List<int>>(grid, ints);
  767. }
  768. private void MenuItemLeftInsert_Click(object sender, RoutedEventArgs e)
  769. {
  770. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  771. int insertIndex = Convert.ToInt32(t.Text);
  772. OnAddStep(insertIndex);
  773. }
  774. private void MenuItemRightInsert_Click(object sender, RoutedEventArgs e)
  775. {
  776. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  777. int insertIndex = Convert.ToInt32(t.Text);
  778. OnAddStep(insertIndex+1);
  779. }
  780. private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
  781. {
  782. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  783. int deleteIndex = Convert.ToInt32(t.Text);
  784. OnDeleteStep(deleteIndex - 1);
  785. }
  786. private void MenuItemCopy_Click(object sender, RoutedEventArgs e)
  787. {
  788. var t= ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  789. copyIndex =Convert.ToInt32( t.Text);
  790. }
  791. /// <summary>
  792. /// 获取父控件
  793. /// </summary>
  794. /// <typeparam name="T"></typeparam>
  795. /// <param name="obj"></param>
  796. /// <param name="name"></param>
  797. /// <returns></returns>
  798. public T GetParentObject2<T>(DependencyObject obj, string name) where T : FrameworkElement
  799. {
  800. DependencyObject parent = VisualTreeHelper.GetParent(obj);
  801. while (parent != null)
  802. {
  803. if (parent is T && (((T)parent).Name == name || string.IsNullOrEmpty(name)))
  804. {
  805. return (T)parent;
  806. }
  807. parent = VisualTreeHelper.GetParent(parent);
  808. }
  809. return null;
  810. }
  811. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  812. {
  813. if (e.RemovedItems.Count > 0)
  814. {
  815. ComboBox comboBox = sender as ComboBox;
  816. Grid grid = GetParentObject<Grid>(comboBox, "");
  817. int index= GridStackPanel.Children.IndexOf(grid);
  818. RecipeStep recipeStep = CurrentRecipe.Steps[index];
  819. recipeStep.LstUnit.RemoveAt(0);
  820. if (comboBox.SelectedIndex == 0)
  821. {
  822. recipeStep.LstUnit.Insert(0, new PressureByPressureModeUnit());
  823. }
  824. else
  825. {
  826. recipeStep.LstUnit.Insert(0, new PressureByValveModeUnit());
  827. }
  828. GridStackPanel.Children.Remove(grid);
  829. GridStackPanel.Children.Insert(index, RecipeStepToGrid(recipeStep).Item1);
  830. }
  831. }
  832. public T GetParentObject<T>(DependencyObject obj, string name) where T : FrameworkElement
  833. {
  834. DependencyObject parent = VisualTreeHelper.GetParent(obj);
  835. while (parent != null)
  836. {
  837. if (parent is T && (((T)parent).Name == name || string.IsNullOrEmpty(name)))
  838. {
  839. return (T)parent;
  840. }
  841. parent = VisualTreeHelper.GetParent(parent);
  842. }
  843. return null;
  844. }
  845. private Tuple<DataGrid,List<int>> RecipeStepToDataGrid(RecipeStep recipeStep)
  846. {
  847. DataGrid dataGrid = new DataGrid();
  848. List<int> ints = new List<int>();
  849. DataTable dataTable = new DataTable();
  850. dataTable.Columns.Add("Key");
  851. dataTable.Columns.Add("Value");
  852. dataGrid.CanUserAddRows = false;
  853. dataGrid.AutoGenerateColumns = true;
  854. dataGrid.SelectionUnit = DataGridSelectionUnit.Cell;
  855. dataGrid.SetValue(System.Windows.Controls.VirtualizingStackPanel.IsVirtualizingProperty, false);
  856. Type recipeType = recipeStep.GetType();
  857. foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
  858. {
  859. if (propertyInfo.Name != "LstUnit")
  860. {
  861. dataTable.Rows.Add(propertyInfo.Name, propertyInfo.GetValue(recipeStep));
  862. }
  863. }
  864. ints.Add(dataTable.Rows.Count);
  865. recipeStep.LstUnit.ToList().ForEach(k =>
  866. {
  867. //List<dynamic> dynamics = new List<dynamic>();
  868. Type type = k.GetType();
  869. ints.Add(type.GetProperties().Length);
  870. foreach (PropertyInfo propertyInfo in type.GetProperties())
  871. {
  872. var name = propertyInfo.Name;
  873. var value = propertyInfo.GetValue(k);
  874. //dynamicModel.PropertyName = name;
  875. //dynamicModel.Property = value;
  876. dataTable.Rows.Add(name, value);
  877. }
  878. });
  879. dataGrid.ItemsSource = dataTable.DefaultView;
  880. //});
  881. return new Tuple<DataGrid, List<int>>(dataGrid, ints);
  882. }
  883. private void ChangeDataGridRowColor(DataGrid dataGrid,List<int> ints)
  884. {
  885. int count = 0;
  886. for (int i = 0; i < ints.Count; i++)
  887. {
  888. for (int j = 0; j < ints[i]; j++)
  889. {
  890. dataGrid.UpdateLayout();
  891. int index = count + j;
  892. DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items[index]) as DataGridRow;
  893. row.Background = solidColorBrushes[i];
  894. }
  895. count += ints[i];
  896. }
  897. }
  898. private void ChangeGridRowColor(Grid grid, List<int> ints)
  899. {
  900. int count = 0;
  901. for (int i = 0; i < ints.Count; i++)
  902. {
  903. for (int j = 0; j < ints[i]; j++)
  904. {
  905. //dataGrid.UpdateLayout();
  906. int index = count + j;
  907. //DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items[index]) as DataGridRow;
  908. //grid.RowDefinitions[index]
  909. }
  910. count += ints[i];
  911. }
  912. }
  913. #region INotifyPropertyChanged
  914. public event PropertyChangedEventHandler PropertyChanged;
  915. public void InvokePropertyChanged(string propertyName)
  916. {
  917. if (PropertyChanged != null)
  918. {
  919. PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
  920. }
  921. }
  922. #endregion INotifyPropertyChanged
  923. }
  924. }