RecipeViewModel.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.UI.View.Common;
  4. //using MECF.Framework.Common.CommonData;
  5. using MECF.Framework.Common.DataCenter;
  6. using MECF.Framework.Common.OperationCenter;
  7. using Microsoft.VisualBasic;
  8. using Newtonsoft.Json;
  9. using Prism.Commands;
  10. using Prism.Mvvm;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Reflection;
  16. using System.Threading;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Data;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Threading;
  23. using System.Xml;
  24. using Venus_Core;
  25. using Venus_Core.Attributes;
  26. using Venus_MainPages.PMs;
  27. using Venus_MainPages.Unity;
  28. using Venus_MainPages.Views;
  29. using Venus_Themes.CustomControls;
  30. using Venus_Themes.UserControls;
  31. using WPF.Themes.UserControls;
  32. namespace Venus_MainPages.ViewModels
  33. {
  34. public enum EPDType
  35. {
  36. Socket,
  37. WCF,
  38. None
  39. }
  40. internal class RecipeViewModel : BindableBase
  41. {
  42. #region 私有字段
  43. public string ModuleName = "PMA";
  44. private string m_CurrentRecipeName;
  45. private UiRecipeManager m_uiRecipeManager = new UiRecipeManager();
  46. private RecipeView recipeView;
  47. private TreeView treeViewRcpList;
  48. private Recipe m_currentRecipe;
  49. private string m_recipeType;
  50. private bool firstLoad=true;
  51. private WrapPanel headWrapPanel;
  52. private StackPanel bodyStackPanel;
  53. private List<SolidColorBrush> solidColorBrushes = new List<SolidColorBrush>()
  54. {
  55. new SolidColorBrush(Colors.Coral),
  56. new SolidColorBrush(Colors.Cyan),
  57. new SolidColorBrush(Colors.Honeydew)
  58. };
  59. private Grid currentRecipeGrid;
  60. private int copyIndex = -1;
  61. private List<string> EPDCfgList=new List<string> ();
  62. private EPDType currentEPDType=EPDType.None;
  63. #endregion
  64. #region 属性
  65. public string CurrentRecipeName
  66. {
  67. get { return m_CurrentRecipeName; }
  68. set { SetProperty(ref m_CurrentRecipeName, value); }
  69. }
  70. public Recipe CurrentRecipe
  71. {
  72. get { return m_currentRecipe; }
  73. set
  74. {
  75. m_currentRecipe = value;
  76. RecipeType = m_currentRecipe.Header.Type.ToString();
  77. }
  78. }
  79. public string RecipeType
  80. {
  81. get { return m_recipeType; }
  82. set { SetProperty(ref m_recipeType, value); }
  83. }
  84. #endregion
  85. #region 命令
  86. private DelegateCommand<Object> _MouseRightButtonDownCommand;
  87. public DelegateCommand<Object> MouseRightButtonDownCommand =>
  88. _MouseRightButtonDownCommand ?? (_MouseRightButtonDownCommand = new DelegateCommand<Object>(OnMouseRightButtonDown));
  89. private DelegateCommand<Object> _LoadedCommand;
  90. public DelegateCommand<Object> LoadedCommand =>
  91. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand<Object>(OnLoaded));
  92. private DelegateCommand _SaveRecipeCommand;
  93. public DelegateCommand SaveRecipeCommand =>
  94. _SaveRecipeCommand ?? (_SaveRecipeCommand = new DelegateCommand(OnSaveRecipe));
  95. private DelegateCommand _AddStepCommand;
  96. public DelegateCommand AddStepCommand =>
  97. _AddStepCommand ?? (_AddStepCommand = new DelegateCommand(OnAddStep));
  98. private DelegateCommand _DeleteStepCommand;
  99. public DelegateCommand DeleteStepCommand =>
  100. _DeleteStepCommand ?? (_DeleteStepCommand = new DelegateCommand(OnDeleteStep));
  101. #endregion
  102. #region 构造函数
  103. public RecipeViewModel()
  104. {
  105. }
  106. #endregion
  107. #region 命令方法
  108. private void OnAddStep()
  109. {
  110. if (CurrentRecipe != null)
  111. {
  112. var index = currentRecipeGrid.ColumnDefinitions.Count;
  113. RecipeStep recipeStep;
  114. if (copyIndex == -1)
  115. {
  116. recipeStep = new RecipeStep();
  117. recipeStep.LstUnit.Add(new PressureByPressureModeUnit());
  118. recipeStep.LstUnit.Add(new TCPUnit());
  119. recipeStep.LstUnit.Add(new BiasUnit());
  120. recipeStep.LstUnit.Add(new GasControlUnit());
  121. recipeStep.LstUnit.Add(new ESCHVUnit());
  122. recipeStep.LstUnit.Add(new ProcessKitUnit());
  123. CurrentRecipe.Steps.Insert(index - 1, recipeStep);
  124. }
  125. else
  126. {
  127. var t = JsonConvert.SerializeObject(CurrentRecipe);
  128. recipeStep = Recipe.Load(t).Steps[copyIndex - 1];
  129. CurrentRecipe.Steps.Insert(index - 1, recipeStep);
  130. }
  131. RecipeStepToGridColumn(recipeStep, currentRecipeGrid, index, true);
  132. for (int i = 1; i <= CurrentRecipe.Steps.Count; i++)
  133. {
  134. CurrentRecipe.Steps[i - 1].StepNo = i;
  135. }
  136. }
  137. }
  138. private void OnDeleteStep()
  139. {
  140. if (CurrentRecipe != null)
  141. {
  142. OnDeleteStep(CurrentRecipe.Steps.Count - 1);
  143. }
  144. }
  145. private void OnSaveRecipe()
  146. {
  147. SaveRecipe(CurrentRecipeName, RecipeUnity.RecipeToString(CurrentRecipe));
  148. }
  149. private void OnLoaded(Object myrecipeView)
  150. {
  151. if (firstLoad == true)
  152. {
  153. firstLoad = false;
  154. recipeView = myrecipeView as RecipeView;
  155. treeViewRcpList = recipeView.treeViewRcpList;
  156. headWrapPanel = recipeView.headWrapPanel;
  157. bodyStackPanel = recipeView.bodyStackPanel;
  158. UpdateRecipeFileList();
  159. treeViewRcpList.SelectedItemChanged += TreeViewRcpList_SelectedItemChanged;
  160. DispatcherTimer timer = new DispatcherTimer();
  161. timer.Interval = TimeSpan.FromSeconds(1);
  162. timer.Tick += Timer_Tick; ;
  163. timer.Start();
  164. for (int i = 0; i < 5; i++)
  165. {
  166. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.EndPoint.SearchCfg");
  167. Thread.Sleep(200);
  168. currentEPDType = (EPDType)System.Convert.ToInt32(QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.EPD.EPDType"));
  169. if (currentEPDType == EPDType.Socket)
  170. {
  171. EPDCfgList = (List<string>)QueryDataClient.Instance.Service.GetData($"{ModuleName}.EPDCfgList");
  172. }
  173. if (EPDCfgList != null)
  174. {
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. private void Timer_Tick(object sender, EventArgs e)
  181. {
  182. }
  183. TreeViewFileItem selectedItem;
  184. private void TreeViewRcpList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  185. {
  186. selectedItem = e.NewValue as TreeViewFileItem;
  187. if (selectedItem == null)
  188. return;
  189. try
  190. {
  191. CurrentRecipeName = selectedItem.FileName;
  192. string xmlRecipeData = m_uiRecipeManager.LoadRecipe(ModuleName, selectedItem.FileName);
  193. if (xmlRecipeData == "")
  194. {
  195. treeViewRcpList.Items.Remove(selectedItem);
  196. return;
  197. }
  198. CurrentRecipe = Recipe.Load(xmlRecipeData);
  199. LoadHeadWrapPanel(headWrapPanel, CurrentRecipe);
  200. LoadRecipe(CurrentRecipe);
  201. }
  202. catch (Exception ex)
  203. {
  204. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  205. }
  206. e.Handled = true;
  207. }
  208. public bool SaveRecipe(string recipeName, string recipeContent)
  209. {
  210. return m_uiRecipeManager.SaveRecipe(ModuleName, recipeName, recipeContent);
  211. }
  212. private void OnMouseRightButtonDown(Object treeView)
  213. {
  214. //treeViewRcpList = treeView as TreeView;
  215. treeViewRcpList.ContextMenu = new ContextMenu() { Background = new SolidColorBrush(Colors.AliceBlue)};
  216. MenuItem menuItem = new MenuItem();
  217. menuItem = new MenuItem();
  218. menuItem.Tag = "\\";
  219. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe);
  220. menuItem.Header = "New Recipe";
  221. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  222. menuItem = new MenuItem();
  223. menuItem.Tag = "\\";
  224. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_DeleteRecipe);
  225. menuItem.Header = "Delete Recipe";
  226. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  227. menuItem = new MenuItem();
  228. menuItem.Tag = "\\";
  229. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_SaveAsRecipe);
  230. menuItem.Header = "Save As Recipe";
  231. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  232. menuItem = new MenuItem();
  233. menuItem.Tag = "\\";
  234. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_RenameRecipe);
  235. menuItem.Header = "Rename";
  236. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  237. treeViewRcpList.ContextMenu.Visibility = Visibility.Visible;
  238. }
  239. private void menuItem_MouseClick_CreateRecipe(object sender, RoutedEventArgs e)
  240. {
  241. MenuItem mit = sender as MenuItem;
  242. string folderName = mit.Tag as string;
  243. PerformCreateRecipe(folderName);
  244. }
  245. private void PerformCreateRecipe(string folderName)
  246. {
  247. try
  248. {
  249. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  250. {
  251. Owner = Application.Current.MainWindow
  252. };
  253. if (dlg.ShowDialog() == true)
  254. {
  255. var recipeName = folderName + "\\" + dlg.InputText;
  256. RecipeType type = (RecipeType)Enum.Parse(typeof(RecipeType), dlg.SelectedType);
  257. string newRecipe =RecipeUnity.CreateRecipe(type, dlg.InputText);
  258. //string recipeContent = m_uiRecipeManager.GetRecipeTemplate(ModuleName);
  259. //m_uiRecipeManager.SaveAsRecipe(ModuleName, recipeName, m_uiRecipeManager.LoadRecipe("system",folderName));
  260. if (SaveAsRecipe(recipeName, newRecipe))
  261. {
  262. //UpdateRecipeFileList();
  263. //SelectRecipe(recipeName);
  264. treeViewRcpList.Items.Add(new TreeViewFileItem(dlg.InputText));
  265. }
  266. else
  267. {
  268. WPFMessageBox.ShowError("Error");
  269. }
  270. }
  271. }
  272. catch (Exception ex)
  273. {
  274. LOG.WriteExeption(ex);
  275. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  276. }
  277. }
  278. private void menuItem_MouseClick_DeleteRecipe(object sender, RoutedEventArgs e)
  279. {
  280. if (CurrentRecipe == null)
  281. {
  282. return;
  283. }
  284. if (WPFMessageBox.ShowQuestion($"Delete {CurrentRecipeName}?","删除后无法恢复!!!") == MessageBoxResult.Yes)
  285. {
  286. MenuItem mit = sender as MenuItem;
  287. //string recipename = mit.Header.ToString();
  288. m_uiRecipeManager.DeleteRecipe(ModuleName, CurrentRecipeName);
  289. //PerformCreateRecipe(folderName);
  290. treeViewRcpList.Items.Remove(selectedItem);
  291. }
  292. }
  293. private void menuItem_MouseClick_SaveAsRecipe(object sender, RoutedEventArgs e)
  294. {
  295. if (CurrentRecipe == null)
  296. {
  297. return;
  298. }
  299. var newName = Interaction.InputBox(" ", "Save As Recipe", CurrentRecipeName, -1, -1);
  300. if (newName != CurrentRecipeName && newName != "")
  301. {
  302. var newRecipe = CurrentRecipe;
  303. newRecipe.Header.Name = newName;
  304. newRecipe.Header.CreateTime = DateTime.Now.ToString();
  305. var newrecipePath = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes", ModuleName, newName + ".rcp");
  306. File.WriteAllText(newrecipePath, RecipeUnity.RecipeToString(newRecipe));
  307. UpdateRecipeFileList();
  308. }
  309. }
  310. private void menuItem_MouseClick_RenameRecipe(object sender, RoutedEventArgs e)
  311. {
  312. if (CurrentRecipe == null)
  313. {
  314. return;
  315. }
  316. var newName= Interaction.InputBox(" ", "Rename Recipe", CurrentRecipeName, -1, -1);
  317. if (newName != CurrentRecipeName && newName!="")
  318. {
  319. var oldrecipePath = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes", ModuleName, CurrentRecipeName + ".rcp");
  320. var newrecipePath = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes", ModuleName, newName + ".rcp");
  321. CurrentRecipe.Header.Name = newName;
  322. SaveRecipe(CurrentRecipeName, RecipeUnity.RecipeToString(CurrentRecipe));
  323. File.Move(oldrecipePath, newrecipePath);
  324. UpdateRecipeFileList();
  325. }
  326. }
  327. public bool SaveAsRecipe(string recipeName, string recipeContent)
  328. {
  329. return m_uiRecipeManager.SaveAsRecipe(ModuleName, recipeName, recipeContent);
  330. }
  331. public string GetXmlRecipeList()
  332. {
  333. return m_uiRecipeManager.GetXmlRecipeList(ModuleName, true) ?? "";
  334. }
  335. void CreateTreeViewItems(XmlElement curElementNode, ItemsControl itemsControl)
  336. {
  337. foreach (XmlElement ele in curElementNode.ChildNodes)
  338. {
  339. if (ele.Name == "File")
  340. {
  341. string fileName = ele.Attributes["Name"].Value;
  342. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  343. TreeViewFileItem item = new TreeViewFileItem(ele.Attributes["Name"].Value);
  344. item.Tag = ele.Attributes["Name"].Value;
  345. //item.ToolTip = fileName;
  346. itemsControl.Items.Add(item);
  347. }
  348. else if (ele.Name == "Folder")
  349. {
  350. string folderName = ele.Attributes["Name"].Value;
  351. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  352. TreeViewFolderItem item = new TreeViewFolderItem(folderName);
  353. item.Tag = ele.Attributes["Name"].Value;
  354. CreateTreeViewItems(ele, item);
  355. item.IsExpanded = false;
  356. itemsControl.Items.Add(item);
  357. }
  358. }
  359. }
  360. private void LoadHeadWrapPanel(WrapPanel HeadWrapPanel, Recipe recipe)
  361. {
  362. HeadWrapPanel.Children.Clear();
  363. Type type = recipe.Header.GetType();
  364. foreach (var propertyInfo in type.GetProperties())
  365. {
  366. TextBlock textBlock = new TextBlock();
  367. textBlock.FontSize = 15;
  368. //textBlock.Width = 100;
  369. textBlock.Text = propertyInfo.Name + ":";
  370. textBlock.Margin = new Thickness(15, 0, 0, 0);
  371. textBlock.VerticalAlignment = VerticalAlignment.Center;
  372. HeadWrapPanel.Children.Add(textBlock);
  373. Binding binding = new Binding()
  374. {
  375. Source = recipe.Header, // 数据源
  376. Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  377. Mode = BindingMode.TwoWay, // 绑定模式
  378. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  379. };
  380. var propertyTypeName = propertyInfo.PropertyType.Name;
  381. var propertyInfoName = propertyInfo.Name;
  382. Control control = new Control();
  383. switch (propertyTypeName)
  384. {
  385. case "Int32":
  386. case "String":
  387. control = new TextBox();
  388. control.Margin = new Thickness(1, 0, 0, 0);
  389. control.BorderThickness = new Thickness(0, 0, 0, 1);
  390. //control.BorderBrush = Brushes.Black;
  391. control.FontSize = 15;
  392. control.Foreground = Brushes.Green;
  393. control.Background = Brushes.Transparent;
  394. control.VerticalAlignment = VerticalAlignment.Center;
  395. control.MinWidth = 15;
  396. control.SetBinding(TextBox.TextProperty, binding);
  397. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  398. if (objAttrs.Length > 0)
  399. {
  400. (control as TextBox).IsReadOnly = true;
  401. }
  402. break;
  403. case "Boolean":
  404. control = new CheckBox();
  405. control.SetBinding(CheckBox.IsCheckedProperty, binding);
  406. break;
  407. default:
  408. control = new ComboBox();
  409. control.Height = 23;
  410. control.SetBinding(ComboBox.SelectedItemProperty, binding);
  411. ItemsControlHelper.SetEnumValuesToItemsSource(control, true);
  412. break;
  413. }
  414. HeadWrapPanel.Children.Add(control);
  415. }
  416. }
  417. private void MenuItemLeftInsert_Click(object sender, RoutedEventArgs e)
  418. {
  419. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  420. int insertIndex = Convert.ToInt32(t.Text);
  421. OnAddStep(insertIndex);
  422. }
  423. private void MenuItemRightInsert_Click(object sender, RoutedEventArgs e)
  424. {
  425. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  426. int insertIndex = Convert.ToInt32(t.Text);
  427. OnAddStep(insertIndex + 1);
  428. }
  429. private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
  430. {
  431. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  432. int deleteIndex = Convert.ToInt32(t.Text);
  433. OnDeleteStep(deleteIndex - 1);
  434. }
  435. private void MenuItemCopy_Click(object sender, RoutedEventArgs e)
  436. {
  437. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  438. copyIndex = Convert.ToInt32(t.Text);
  439. }
  440. #endregion
  441. #region 私有方法
  442. private void RecipeStepToGridColumn(RecipeStep recipeStep, Grid grid, int index, bool isInsert)
  443. {
  444. int location = 1;
  445. if (isInsert == true)
  446. {
  447. location -= 1;
  448. }
  449. Type recipeType = recipeStep.GetType();
  450. int cycleCount = 1;
  451. if (index == 0)
  452. {
  453. cycleCount = 2;
  454. }
  455. for (int j = 0; j < cycleCount; j++)
  456. {
  457. int i = 0;
  458. ColumnDefinition col1 = new ColumnDefinition();
  459. //col1.MinWidth = 50;
  460. grid.ColumnDefinitions.Insert(index, col1);
  461. Binding stepcheckbinding = null;
  462. //grid.MinWidth = 200;
  463. foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
  464. {
  465. string propertyInfoName = propertyInfo.Name;
  466. string propertyTypeName = propertyInfo.PropertyType.Name;
  467. if (propertyInfoName != "LstUnit")
  468. {
  469. if (index == 0 && grid.ColumnDefinitions.Count == 1)
  470. {
  471. RowDefinition row1 = new RowDefinition();
  472. grid.RowDefinitions.Add(row1);
  473. }
  474. if (grid.ColumnDefinitions.Count == 1 && j == 0)
  475. {
  476. TextBox textBlock = new TextBox();
  477. textBlock.IsReadOnly = true;
  478. textBlock.Text = propertyInfoName;
  479. grid.Children.Add(textBlock);
  480. textBlock.Background = new SolidColorBrush(Colors.BurlyWood);
  481. Grid.SetRow(textBlock, i);
  482. Grid.SetColumn(textBlock, 0);
  483. }
  484. else
  485. {
  486. Binding binding = new Binding()
  487. {
  488. Source = recipeStep, // 数据源
  489. Path = new PropertyPath(propertyInfoName), // 需绑定的数据源属性名
  490. Mode = BindingMode.TwoWay, // 绑定模式
  491. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  492. };
  493. switch (propertyTypeName)
  494. {
  495. case "Int32":
  496. case "String":
  497. if (propertyInfoName == "EPDConfig")
  498. {
  499. if (currentEPDType == EPDType.Socket)
  500. {
  501. ComboBox EPDcomboBox = new ComboBox();
  502. EPDcomboBox.Style = (Style)recipeView.FindResource("customeComboBoxStyle");
  503. EPDcomboBox.Background = new SolidColorBrush(Colors.Black);
  504. EPDcomboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  505. EPDcomboBox.ItemsSource = EPDCfgList;
  506. grid.Children.Add(EPDcomboBox);
  507. Grid.SetRow(EPDcomboBox, i);
  508. Grid.SetColumn(EPDcomboBox, index + location);
  509. EPDcomboBox.DropDownOpened += EPDcomboBox_DropDownOpened;
  510. }
  511. else if (currentEPDType == EPDType.WCF)
  512. {
  513. Button EPDButton = new Button();
  514. EPDButton.Content = "EndPoint";
  515. EPDButton.Cursor = Cursors.Hand;
  516. EPDButton.Tag = $"{CurrentRecipeName}.{(index + location).ToString()}";
  517. EPDButton.Style = null;
  518. EPDButton.Click += EPDButton_Click;
  519. grid.Children.Add(EPDButton);
  520. Grid.SetRow(EPDButton, i);
  521. Grid.SetColumn(EPDButton, index + location);
  522. }
  523. break;
  524. }
  525. TextBox textBox = new TextBox();
  526. if (stepcheckbinding == null)
  527. {
  528. textBox.IsEnabled = true;
  529. }
  530. else
  531. {
  532. textBox.SetBinding(TextBox.IsEnabledProperty, stepcheckbinding);
  533. }
  534. textBox.SetBinding(TextBox.TextProperty, binding);
  535. grid.Children.Add(textBox);
  536. Grid.SetRow(textBox, i);
  537. Grid.SetColumn(textBox, index + location);
  538. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  539. if (objAttrs.Length > 0)
  540. {
  541. textBox.IsReadOnly = true;
  542. ContextMenu contextmenu = new ContextMenu();
  543. textBox.ContextMenu = contextmenu;
  544. MenuItem menuItemDelete = new MenuItem();
  545. menuItemDelete.Header = "删除";
  546. menuItemDelete.Click += MenuItemDelete_Click;
  547. MenuItem menuItemLeftInsert = new MenuItem();
  548. menuItemLeftInsert.Header = "左插入";
  549. menuItemLeftInsert.Click += MenuItemLeftInsert_Click;
  550. MenuItem menuItemRightInsert = new MenuItem();
  551. menuItemRightInsert.Header = "右插入";
  552. menuItemRightInsert.Click += MenuItemRightInsert_Click;
  553. MenuItem menuItemCopy = new MenuItem();
  554. menuItemCopy.Tag = textBox.Text;
  555. menuItemCopy.Header = "复制";
  556. menuItemCopy.Click += MenuItemCopy_Click;
  557. contextmenu.Items.Add(menuItemCopy);
  558. contextmenu.Items.Add(menuItemDelete);
  559. contextmenu.Items.Add(menuItemLeftInsert);
  560. contextmenu.Items.Add(menuItemRightInsert);
  561. }
  562. break;
  563. case "Boolean":
  564. CheckBox checkBox = new CheckBox();
  565. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  566. grid.Children.Add(checkBox);
  567. Grid.SetRow(checkBox, i);
  568. Grid.SetColumn(checkBox, index + location);
  569. if (stepcheckbinding == null)
  570. {
  571. stepcheckbinding = new Binding
  572. {
  573. Source = checkBox, // 数据源
  574. Path = new PropertyPath("IsChecked"), // 需绑定的数据源属性名
  575. Mode = BindingMode.TwoWay, // 绑定模式
  576. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  577. };
  578. }
  579. else
  580. {
  581. if (stepcheckbinding == null)
  582. {
  583. checkBox.IsEnabled = true;
  584. }
  585. else
  586. {
  587. checkBox.SetBinding(TextBox.IsEnabledProperty, stepcheckbinding);
  588. }
  589. }
  590. break;
  591. default:
  592. ComboBox comboBox = new ComboBox();
  593. comboBox.Style = (Style)recipeView.FindResource("customeComboBoxStyle");
  594. comboBox.Background = new SolidColorBrush(Colors.Black);
  595. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  596. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  597. grid.Children.Add(comboBox);
  598. Grid.SetRow(comboBox, i);
  599. Grid.SetColumn(comboBox, index + location);
  600. if (stepcheckbinding == null)
  601. {
  602. comboBox.IsEnabled = true;
  603. }
  604. else
  605. {
  606. comboBox.SetBinding(TextBox.IsEnabledProperty, stepcheckbinding);
  607. }
  608. break;
  609. }
  610. }
  611. i++;
  612. }
  613. }
  614. int k = 0;
  615. recipeStep.LstUnit.ToList().ForEach(x =>
  616. {
  617. Type unitType = x.GetType();
  618. Binding checkbinding = null;
  619. foreach (PropertyInfo propertyInfo in unitType.GetProperties())
  620. {
  621. if (index == 0 && grid.ColumnDefinitions.Count == 1)
  622. {
  623. RowDefinition row1 = new RowDefinition();
  624. grid.RowDefinitions.Add(row1);
  625. }
  626. if (grid.ColumnDefinitions.Count == 1 && j == 0)
  627. {
  628. TextBox textBlock = new TextBox();
  629. textBlock.IsReadOnly = true;
  630. switch (propertyInfo.Name)
  631. {
  632. case "Gas1":
  633. var data1 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas1");
  634. textBlock.Text = $"{propertyInfo.Name}({data1.DisplayName},{data1.Scale})";
  635. break;
  636. case "Gas2":
  637. var data2 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas2");
  638. textBlock.Text = $"{propertyInfo.Name}({data2.DisplayName},{data2.Scale})";
  639. break;
  640. case "Gas3":
  641. var data3 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas3");
  642. textBlock.Text = $"{propertyInfo.Name}({data3.DisplayName},{data3.Scale})";
  643. break;
  644. case "Gas4":
  645. var data4 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas4");
  646. textBlock.Text = $"{propertyInfo.Name}({data4.DisplayName},{data4.Scale})";
  647. break;
  648. case "Gas5":
  649. var data5 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas5");
  650. textBlock.Text = $"{propertyInfo.Name}({data5.DisplayName},{data5.Scale})";
  651. break;
  652. case "Gas6":
  653. var data6 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas6");
  654. textBlock.Text = $"{propertyInfo.Name}({data6.DisplayName},{data6.Scale})";
  655. break;
  656. case "Gas7":
  657. var data7 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas7");
  658. textBlock.Text = $"{propertyInfo.Name}({data7.DisplayName},{data7.Scale})";
  659. break;
  660. case "Gas8":
  661. var data8 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas8");
  662. textBlock.Text = $"{propertyInfo.Name}({data8.DisplayName},{data8.Scale})";
  663. break;
  664. default:
  665. textBlock.Text = propertyInfo.Name;
  666. break;
  667. }
  668. textBlock.Background = solidColorBrushes[k % 3];
  669. grid.Children.Add(textBlock);
  670. Grid.SetRow(textBlock, i);
  671. Grid.SetColumn(textBlock, 0);
  672. }
  673. else
  674. {
  675. Binding binding = new Binding()
  676. {
  677. Source = recipeStep.LstUnit[k], // 数据源
  678. //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  679. Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  680. Mode = BindingMode.TwoWay, // 绑定模式
  681. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  682. };
  683. var item = propertyInfo.PropertyType.Name;
  684. if (propertyInfo.Name == "UnitName")
  685. {
  686. TextBox textBlock1 = new TextBox();
  687. textBlock1.Text = propertyInfo.GetValue(x).ToString();
  688. textBlock1.IsReadOnly = true;
  689. grid.Children.Add(textBlock1);
  690. Grid.SetRow(textBlock1, i);
  691. Grid.SetColumn(textBlock1, index + location);
  692. }
  693. else
  694. {
  695. switch (item)
  696. {
  697. case "Int32":
  698. case "String":
  699. TextBox textBox = new TextBox();
  700. if (checkbinding == null)
  701. {
  702. textBox.IsEnabled = true;
  703. }
  704. else
  705. {
  706. textBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
  707. }
  708. switch (propertyInfo.Name)
  709. {
  710. case "Gas1":
  711. var data1 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas1");
  712. TextBoxMaxValue.SetMaxValue(textBox, (int)data1.Scale);
  713. break;
  714. case "Gas2":
  715. var data2 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas2");
  716. TextBoxMaxValue.SetMaxValue(textBox, (int)data2.Scale);
  717. break;
  718. case "Gas3":
  719. var data3 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas3");
  720. break;
  721. case "Gas4":
  722. var data4 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas4");
  723. break;
  724. case "Gas5":
  725. var data5 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas5");
  726. break;
  727. case "Gas6":
  728. var data6 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas6");
  729. break;
  730. case "Gas7":
  731. var data7 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas7");
  732. break;
  733. case "Gas8":
  734. var data8 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas8");
  735. break;
  736. }
  737. textBox.SetBinding(TextBox.TextProperty, binding);
  738. grid.Children.Add(textBox);
  739. Grid.SetRow(textBox, i);
  740. Grid.SetColumn(textBox, index + location);
  741. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  742. if (objAttrs.Length > 0)
  743. {
  744. textBox.IsReadOnly = true;
  745. }
  746. break;
  747. case "Boolean":
  748. CheckBox checkBox = new CheckBox();
  749. if (checkbinding == null)
  750. {
  751. checkbinding = new Binding
  752. {
  753. Source = checkBox, // 数据源
  754. Path = new PropertyPath("IsChecked"), // 需绑定的数据源属性名
  755. Mode = BindingMode.TwoWay, // 绑定模式
  756. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  757. };
  758. }
  759. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  760. grid.Children.Add(checkBox);
  761. Grid.SetRow(checkBox, i);
  762. Grid.SetColumn(checkBox, index + location);
  763. break;
  764. default:
  765. ComboBox comboBox = new ComboBox();
  766. comboBox.Style = (Style)recipeView.FindResource("customeComboBoxStyle");
  767. if (checkbinding == null)
  768. {
  769. comboBox.IsEnabled = true;
  770. }
  771. else
  772. {
  773. comboBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
  774. }
  775. comboBox.Background = Brushes.White;
  776. string path = propertyInfo.Name;
  777. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  778. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  779. grid.Children.Add(comboBox);
  780. Grid.SetRow(comboBox, i);
  781. Grid.SetColumn(comboBox, index + location);
  782. break;
  783. }
  784. }
  785. }
  786. i++;
  787. }
  788. k++;
  789. });
  790. //index++;
  791. }
  792. }
  793. private void EPDcomboBox_DropDownOpened(object sender, EventArgs e)
  794. {
  795. }
  796. private void EPDButton_Click(object sender, RoutedEventArgs e)
  797. {
  798. Button button = sender as Button;
  799. string para = button.Tag.ToString();
  800. int stepno = Convert.ToInt32(para.Split('.')[1])-1;
  801. var endpointconfigitem = new MECF.Framework.Common.CommonData.EndPointConfigItem();
  802. if (CurrentRecipe.Steps[stepno].EPDConfig!=null && CurrentRecipe.Steps[stepno].EPDConfig.Length>20)
  803. {
  804. endpointconfigitem.SetValue(CurrentRecipe.Steps[stepno].EPDConfig);
  805. }
  806. (new EndPointDlg(para, endpointconfigitem,CurrentRecipe)).ShowDialog();
  807. }
  808. private void UpdateRecipeFileList()
  809. {
  810. XmlDocument doc = new XmlDocument();
  811. doc.LoadXml(GetXmlRecipeList());
  812. treeViewRcpList.Items.Clear();
  813. CreateTreeViewItems(doc.DocumentElement, this.treeViewRcpList);
  814. }
  815. private void LoadRecipe(Recipe recipe)
  816. {
  817. copyIndex = -1;
  818. bodyStackPanel.Children.Clear();
  819. //CurrentRecipe = null;
  820. GC.Collect(); // This should pick up the control removed at a previous MouseDown
  821. GC.WaitForPendingFinalizers(); // Doesn't help either
  822. GC.Collect();
  823. GC.WaitForPendingFinalizers(); // Doesn't help either
  824. CurrentRecipe = recipe;
  825. Grid grid = new Grid();
  826. grid.Margin = new Thickness(15);
  827. //index = 0;
  828. for (int i = 0; i < recipe.Steps.Count; i++)
  829. {
  830. RecipeStepToGridColumn(recipe.Steps[i], grid, i, false);
  831. }
  832. bodyStackPanel.Children.Add(grid);
  833. currentRecipeGrid = grid;
  834. }
  835. private void OnAddStep(int index)
  836. {
  837. if (CurrentRecipe != null)
  838. {
  839. RecipeStep recipeStep;
  840. if (copyIndex == -1)
  841. {
  842. recipeStep = new RecipeStep();
  843. recipeStep.LstUnit.Add(new PressureByPressureModeUnit());
  844. recipeStep.LstUnit.Add(new TCPUnit());
  845. recipeStep.LstUnit.Add(new BiasUnit());
  846. recipeStep.LstUnit.Add(new GasControlUnit());
  847. recipeStep.LstUnit.Add(new ESCHVUnit());
  848. recipeStep.LstUnit.Add(new ProcessKitUnit());
  849. CurrentRecipe.Steps.Insert(index - 1, recipeStep);
  850. }
  851. else
  852. {
  853. var t = JsonConvert.SerializeObject(CurrentRecipe);
  854. recipeStep = Recipe.Load(t).Steps[copyIndex - 1];
  855. CurrentRecipe.Steps.Insert(index - 1, recipeStep);
  856. }
  857. RecipeStepToGridColumn(recipeStep, currentRecipeGrid, index, true);
  858. for (int i = 1; i <= CurrentRecipe.Steps.Count; i++)
  859. {
  860. CurrentRecipe.Steps[i - 1].StepNo = i;
  861. }
  862. }
  863. }
  864. private void OnDeleteStep(int index)
  865. {
  866. if (CurrentRecipe != null && CurrentRecipe.Steps.Count > 1)
  867. {
  868. CurrentRecipe.Steps.RemoveAt(index);
  869. currentRecipeGrid.Children.RemoveRange(currentRecipeGrid.RowDefinitions.Count * (index + 1), currentRecipeGrid.RowDefinitions.Count);
  870. currentRecipeGrid.ColumnDefinitions.RemoveAt(index);
  871. for (int i = 1; i <= CurrentRecipe.Steps.Count; i++)
  872. {
  873. CurrentRecipe.Steps[i - 1].StepNo = i;
  874. }
  875. }
  876. }
  877. #endregion
  878. }
  879. }