RecipeEditorView.xaml.cs 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Xml;
  12. using Aitex.Core.Account;
  13. using Aitex.Core.RT.Log;
  14. using Aitex.Core.UI.View.Common;
  15. using Aitex.UI.RecipeEditor.View;
  16. using ExcelLibrary.SpreadSheet;
  17. using MECF.Framework.Common.DataCenter;
  18. using MECF.Framework.Common.Equipment;
  19. using MECF.Framework.UI.Core.Accounts;
  20. namespace VirgoUI.Client.Models.PMs
  21. {
  22. /// <summary>
  23. /// Interaction logic for RecipeEditorView.xaml
  24. /// </summary>
  25. public partial class RecipeEditorView : UserControl
  26. {
  27. private string _culture;
  28. private string tragetFilePath = "";
  29. private object OriginalItem;
  30. private RecipeEditorViewModel _viewModel;
  31. private int MenuPermission;
  32. private ViewPermission _permission;
  33. private Point _lastMouseDown;
  34. public RecipeEditorView()
  35. {
  36. InitializeComponent();
  37. this.buttonSaveTableRecipe.ContextMenu = null;
  38. this.DataContext = _viewModel = new RecipeEditorViewModel(new UiRecipeManager(), ModuleName.System.ToString());
  39. this.Loaded += new RoutedEventHandler(RecipeEditorView_Loaded);
  40. this.Unloaded += new RoutedEventHandler(RecipeEditorView_Unloaded);
  41. //VirgoUiSystem.Instance.CultureChanged += CultureChanged;
  42. }
  43. private void CultureChanged(string s)
  44. {
  45. _culture = s;
  46. this.tableRecipeGrid.UpdateCultureResource(s);
  47. }
  48. static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
  49. {
  50. while (source != null && source.GetType() != typeof(T))
  51. source = VisualTreeHelper.GetParent(source);
  52. return source;
  53. }
  54. void RecipeEditorView_Unloaded(object sender, RoutedEventArgs e)
  55. {
  56. }
  57. void RecipeEditorView_Loaded(object sender, RoutedEventArgs e)
  58. {
  59. buttonSaveTableRecipe.Click += (obj, args) => { SetMenu.IsOpen = true; };
  60. SaveToSingle.Header = $"Save To {((RecipeEditorViewModel)this.DataContext).SystemName}";
  61. SaveToDouble.Header = "Save To PMA And PMB";
  62. _viewModel.ChamberId = ((RecipeEditorViewModel)this.DataContext).SystemName;
  63. MenuPermission = ClientApp.Instance.GetPermission($"Recipe{_viewModel.ChamberId}");
  64. if (_culture != Thread.CurrentThread.CurrentCulture.Name)
  65. {
  66. _culture = Thread.CurrentThread.CurrentCulture.Name;
  67. this.tableRecipeGrid.UpdateCultureResource(_culture);
  68. }
  69. _viewModel.UpdateAllConfig();
  70. tableRecipeGrid.SetEnableBarcode(_viewModel.IsBarcodeEnabled);
  71. tableRecipeGrid.SetUser(ClientApp.Instance.UserContext.LoginName);
  72. tableRecipeGrid.SetEndPointDefaultValue((string)QueryDataClient.Instance.Service.GetConfig("System.EndPoint.EndPointDefaultValue"));
  73. UpdateRecipeFileList();
  74. this.Name = "recipeEditor";
  75. _permission = ViewPermission.FullyControl;//this.GetPermission();
  76. switch (_permission)
  77. {
  78. case ViewPermission.Readonly:
  79. buttonReloadTableRecipe.IsEnabled = false;
  80. tableRecipeGrid.IsEnabled = false;
  81. buttonSaveTableRecipe.IsEnabled = false;
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. private void treeViewRcpList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  88. {
  89. //var permission = this.GetPermission();
  90. if (tableRecipeGrid.IsRecipeModified)
  91. {
  92. //if (permission == ViewPermission.FullyControl)
  93. {
  94. switch (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgSaveCurrentRecipe"].ToString(), _viewModel.CurrentRecipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNo, MessageBoxImage.Question))
  95. {
  96. case MessageBoxResult.Yes:
  97. if (SaveRecipe())
  98. {
  99. var item = e.NewValue as TreeViewFileItem;
  100. if (item != null)
  101. item.IsSelected = true;
  102. }
  103. else
  104. {
  105. var item = e.OldValue as TreeViewFileItem;
  106. if (item != null)
  107. item.IsSelected = true;
  108. return;
  109. }
  110. break;
  111. case MessageBoxResult.No:
  112. {
  113. var item = e.NewValue as TreeViewFileItem;
  114. if (item != null)
  115. item.IsSelected = true;
  116. }
  117. break;
  118. }
  119. }
  120. }
  121. var selectedItem = e.NewValue as TreeViewFileItem;
  122. if (selectedItem == null)
  123. return;
  124. try
  125. {
  126. this._viewModel.CurrentRecipeName = selectedItem.FileName;
  127. string xmlRecipeData = _viewModel.LoadRecipe(selectedItem.FileName);
  128. this.tableRecipeGrid.ControlViewModel.LoadRecipe(_viewModel.RecipeFormat, xmlRecipeData);
  129. }
  130. catch (Exception ex)
  131. {
  132. LOG.Write(ex);
  133. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  134. }
  135. }
  136. private bool SaveRecipe(string flag = "Single")
  137. {
  138. bool ret = true;
  139. try
  140. {
  141. var hasErr = this.tableRecipeGrid.ControlViewModel.Errors.Count > 0;
  142. if (!hasErr ||
  143. MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeSaveInfo"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes)
  144. {
  145. if (flag == "Single")
  146. ret = _viewModel.SaveRecipe(this._viewModel.CurrentRecipeName, this.tableRecipeGrid.ControlViewModel.GetRecipeContentString());
  147. else if (flag == "Double")
  148. ret = _viewModel.SaveRecipeDouble(this._viewModel.CurrentRecipeName, this.tableRecipeGrid.ControlViewModel.GetRecipeContentString());
  149. if (ret)
  150. {
  151. tableRecipeGrid.Dispatcher.Invoke(new System.Action(() =>
  152. {
  153. this.tableRecipeGrid.ControlViewModel.LoadRecipe(_viewModel.RecipeFormat, _viewModel.LoadRecipe(_viewModel.CurrentRecipeName));
  154. }));
  155. }
  156. }
  157. else
  158. {
  159. ret = false;
  160. }
  161. }
  162. catch (Exception ex)
  163. {
  164. LOG.Write(ex);
  165. MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeSaveException"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  166. ret = false;
  167. }
  168. return ret;
  169. }
  170. private void treeViewRcpList_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  171. {
  172. if (_permission != ViewPermission.FullyControl)
  173. return;
  174. //black place right mouse click
  175. treeViewRcpList.ContextMenu = new ContextMenu();
  176. MenuItem menuItem = new MenuItem();
  177. menuItem = new MenuItem();
  178. menuItem.Tag = "\\";
  179. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe);
  180. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewRecipe"];
  181. this.treeViewRcpList.ContextMenu.Items.Add(menuItem);
  182. menuItem = new MenuItem();
  183. menuItem.Tag = "\\";
  184. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ImportRecipe);
  185. menuItem.Header = Application.Current.Resources["GlobalLableMenuImportRecipe"];
  186. this.treeViewRcpList.ContextMenu.Items.Add(menuItem);
  187. this.treeViewRcpList.ContextMenu.Items.Add(new Separator());
  188. menuItem = new MenuItem();
  189. menuItem.Tag = "\\";
  190. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateFolder);
  191. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewFolder"];
  192. this.treeViewRcpList.ContextMenu.Items.Add(menuItem);
  193. treeViewRcpList.ContextMenu.Visibility = Visibility.Visible;
  194. }
  195. private void TreeViewItem_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  196. {
  197. if (_permission != ViewPermission.FullyControl)
  198. return;
  199. var treeViewFileItem = VisualUpwardSearch<TreeViewFileItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
  200. if (treeViewFileItem != null)
  201. {
  202. treeViewFileItem.Focus();
  203. treeViewFileItem.IsSelected = true;
  204. treeViewFileItem.ContextMenu = new ContextMenu();
  205. MenuItem menuItem = null;
  206. menuItem = new MenuItem();
  207. menuItem.Tag = treeViewFileItem.Tag as string;
  208. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ExportRecipe);
  209. menuItem.Header = Application.Current.Resources["GlobalLableMenuExportRecipe"];
  210. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  211. menuItem = new MenuItem();
  212. menuItem.Tag = treeViewFileItem.Tag as string;
  213. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ExportExcel);
  214. menuItem.Header = Application.Current.Resources["GlobalLableMenuExportRecipeAsExcel"];
  215. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  216. menuItem = new MenuItem();
  217. menuItem.Tag = treeViewFileItem.Tag as string;
  218. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_SaveAs);
  219. menuItem.Header = Application.Current.Resources["GlobalLableMenuSaveAs"];
  220. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  221. menuItem = new MenuItem();
  222. menuItem.Tag = treeViewFileItem.Tag as string;
  223. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_RenameRecipe);
  224. menuItem.Header = Application.Current.Resources["GlobalLableMenuRenameRecipe"];
  225. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  226. menuItem = new MenuItem();
  227. menuItem.Tag = treeViewFileItem.Tag as string;
  228. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_DeleteRecipe);
  229. menuItem.Header = Application.Current.Resources["GlobalLableMenuDeleteRecipe"];
  230. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  231. treeViewFileItem.ContextMenu.Visibility = Visibility.Visible;
  232. }
  233. else
  234. {
  235. var treeViewFolderItem = VisualUpwardSearch<TreeViewFolderItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
  236. if (treeViewFolderItem != null)
  237. {
  238. treeViewFolderItem.Focus();
  239. treeViewFolderItem.IsSelected = true;
  240. treeViewFolderItem.ContextMenu = new ContextMenu();
  241. MenuItem menuItem = null;
  242. menuItem = new MenuItem();
  243. menuItem.Tag = treeViewFolderItem.Tag as string;
  244. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe);
  245. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewRecipe"];
  246. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  247. menuItem = new MenuItem();
  248. menuItem.Tag = treeViewFolderItem.Tag as string;
  249. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ImportRecipe);
  250. menuItem.Header = Application.Current.Resources["GlobalLableMenuImportRecipe"];
  251. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  252. treeViewFolderItem.ContextMenu.Items.Add(new Separator());
  253. menuItem = new MenuItem();
  254. menuItem.Tag = treeViewFolderItem.Tag as string;
  255. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateFolder);
  256. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewFolder"];
  257. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  258. menuItem = new MenuItem();
  259. menuItem.Tag = treeViewFolderItem.Tag as string;
  260. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_RenameFolder);
  261. menuItem.Header = Application.Current.Resources["GlobalLableMenuRenameFolder"];
  262. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  263. menuItem = new MenuItem();
  264. menuItem.Tag = treeViewFolderItem.Tag as string;
  265. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_DeleteFolder);
  266. menuItem.Header = Application.Current.Resources["GlobalLableMenuDeleteFolder"];
  267. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  268. treeViewFolderItem.ContextMenu.Visibility = Visibility.Visible;
  269. }
  270. }
  271. }
  272. /// <summary>
  273. /// reload current recipe content
  274. /// </summary>
  275. /// <param name="sender"></param>
  276. /// <param name="e"></param>
  277. private void buttonCodeReload_Click(object sender, RoutedEventArgs e)
  278. {
  279. if (MenuPermission != 3) return;
  280. if (MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeReloadInfo"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes)
  281. {
  282. this.tableRecipeGrid.Dispatcher.Invoke(new System.Action(() =>
  283. {
  284. this.tableRecipeGrid.ControlViewModel.LoadRecipe(_viewModel.RecipeFormat, _viewModel.LoadRecipe(_viewModel.CurrentRecipeName));
  285. }));
  286. }
  287. }
  288. ///// <summary>
  289. ///// Save recipe file
  290. ///// </summary>
  291. ///// <param name="sender"></param>
  292. ///// <param name="e"></param>
  293. //private void buttonCodeSave_Click(object sender, RoutedEventArgs e)
  294. //{
  295. // SaveRecipe();
  296. //}
  297. /// <summary>
  298. /// Save recipe file
  299. /// </summary>
  300. /// <param name="sender"></param>
  301. /// <param name="e"></param>
  302. private void SaveToSingle_Click(object sender, RoutedEventArgs e)
  303. {
  304. if (MenuPermission != 3) return;
  305. SaveRecipe();
  306. }
  307. /// <summary>
  308. /// Save recipe file
  309. /// </summary>
  310. /// <param name="sender"></param>
  311. /// <param name="e"></param>
  312. private void SaveToDouble_Click(object sender, RoutedEventArgs e)
  313. {
  314. if (MenuPermission != 3) return;
  315. SaveRecipe("Double");
  316. }
  317. void SelectRecipe(string recipeName)
  318. {
  319. try
  320. {
  321. string[] paths = recipeName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
  322. string fileName = "";
  323. for (int i = 0; i < paths.Length - 1; i++)
  324. fileName += paths[i] + "\\";
  325. fileName += paths[paths.Length - 1];
  326. selectRecipe(treeViewRcpList, paths, 0, fileName);
  327. }
  328. catch (Exception ex)
  329. {
  330. LOG.Write(ex);
  331. }
  332. }
  333. ItemsControl selectRecipe(ItemsControl currentNode, string[] paths, int index, string fileName)
  334. {
  335. if (currentNode == null)
  336. return null;
  337. if (index == paths.Length - 1)
  338. {
  339. foreach (var item in currentNode.Items)
  340. {
  341. TreeViewFileItem tvf = item as TreeViewFileItem;
  342. if (tvf != null && tvf.FileName == fileName)
  343. {
  344. tvf.IsSelected = true;
  345. return null;
  346. }
  347. }
  348. }
  349. foreach (var item in currentNode.Items)
  350. {
  351. TreeViewFolderItem tvf = item as TreeViewFolderItem;
  352. if (tvf != null && tvf.FolderName == paths[index])
  353. {
  354. tvf.IsExpanded = true;
  355. selectRecipe(tvf, paths, index + 1, fileName);
  356. break;
  357. }
  358. }
  359. return null;
  360. }
  361. private void UpdateRecipeFileList()
  362. {
  363. XmlDocument doc = new XmlDocument();
  364. doc.LoadXml(_viewModel.GetXmlRecipeList());
  365. this.treeViewRcpList.Items.Clear();
  366. CreateTreeViewItems(doc.DocumentElement, this.treeViewRcpList);
  367. }
  368. void CreateTreeViewItems(XmlElement curElementNode, ItemsControl itemsControl)
  369. {
  370. foreach (XmlElement ele in curElementNode.ChildNodes)
  371. {
  372. if (ele.Name == "File")
  373. {
  374. string fileName = ele.Attributes["Name"].Value;
  375. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  376. TreeViewFileItem item = new TreeViewFileItem(ele.Attributes["Name"].Value);
  377. item.Tag = ele.Attributes["Name"].Value;
  378. item.ToolTip = fileName;
  379. itemsControl.Items.Add(item);
  380. }
  381. else if (ele.Name == "Folder")
  382. {
  383. string folderName = ele.Attributes["Name"].Value;
  384. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  385. TreeViewFolderItem item = new TreeViewFolderItem(folderName);
  386. item.Tag = ele.Attributes["Name"].Value;
  387. CreateTreeViewItems(ele, item);
  388. item.IsExpanded = false;
  389. itemsControl.Items.Add(item);
  390. }
  391. }
  392. }
  393. void menuItem_MouseClick_SaveAs(object sender, RoutedEventArgs e)
  394. {
  395. try
  396. {
  397. MenuItem mit = sender as MenuItem;
  398. var oldName = mit.Tag as string;
  399. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  400. {
  401. InputText = oldName,
  402. Owner = Application.Current.MainWindow
  403. };
  404. var ret = dlg.ShowDialog();
  405. if (ret.HasValue && ret.Value)
  406. {
  407. var newName = dlg.InputText;
  408. if (_viewModel.SaveAsRecipe(newName, _viewModel.LoadRecipe(oldName)))
  409. {
  410. UpdateRecipeFileList();
  411. SelectRecipe(newName);
  412. }
  413. else
  414. {
  415. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeSaveAsFailed"].ToString(), newName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  416. }
  417. }
  418. }
  419. catch (Exception ex)
  420. {
  421. LOG.Write(ex);
  422. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeSaveAsException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  423. }
  424. }
  425. void menuItem_MouseClick_CreateFolder(object sender, RoutedEventArgs e)
  426. {
  427. try
  428. {
  429. MenuItem mit = sender as MenuItem;
  430. string folderName = mit.Tag as string;
  431. var selectedRecipeItem = treeViewRcpList.SelectedItem as TreeViewFileItem;
  432. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputFolderName"].ToString())
  433. {
  434. Owner = Application.Current.MainWindow
  435. };
  436. if (dlg.ShowDialog() == true)
  437. {
  438. this._viewModel.CreateFolder(folderName + "\\" + dlg.InputText);
  439. UpdateRecipeFileList();
  440. if (selectedRecipeItem != null)
  441. SelectRecipe(selectedRecipeItem.FileName);
  442. }
  443. }
  444. catch (Exception ex)
  445. {
  446. LOG.Write(ex);
  447. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeFolderNewFailed"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString());
  448. }
  449. }
  450. /// <summary>
  451. /// Create new recipe
  452. /// </summary>
  453. /// <param name="sender"></param>
  454. /// <param name="e"></param>
  455. void menuItem_MouseClick_CreateRecipe(object sender, RoutedEventArgs e)
  456. {
  457. MenuItem mit = sender as MenuItem;
  458. string folderName = mit.Tag as string;
  459. PerformCreateRecipe(folderName);
  460. }
  461. /// <summary>
  462. /// delete folder
  463. /// </summary>
  464. /// <param name="sender"></param>
  465. /// <param name="e"></param>
  466. void menuItem_MouseClick_DeleteFolder(object sender, RoutedEventArgs e)
  467. {
  468. MenuItem mit = sender as MenuItem;
  469. string folderName = mit.Tag as string;
  470. PerformDeleteFolder(folderName);
  471. }
  472. private void PerformDeleteFolder(string folderName)
  473. {
  474. try
  475. {
  476. var selectedRecipeItem = treeViewRcpList.SelectedItem as TreeViewFileItem;
  477. if (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteFolderInfo"].ToString(), folderName),
  478. Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  479. {
  480. this._viewModel.DeleteFolder(folderName);
  481. UpdateRecipeFileList();
  482. if (selectedRecipeItem != null)
  483. SelectRecipe(selectedRecipeItem.FileName);
  484. }
  485. }
  486. catch (Exception ex)
  487. {
  488. LOG.Write(ex);
  489. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteFolderException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  490. }
  491. }
  492. /// <summary>
  493. /// rename folder
  494. /// </summary>
  495. /// <param name="sender"></param>
  496. /// <param name="e"></param>
  497. void menuItem_MouseClick_RenameFolder(object sender, RoutedEventArgs e)
  498. {
  499. MenuItem mit = sender as MenuItem;
  500. string folderName = mit.Tag as string;
  501. PerformRenameFolder(folderName);
  502. }
  503. private void PerformRenameFolder(string folderName)
  504. {
  505. try
  506. {
  507. int lastSlashIndex = folderName.LastIndexOf('\\');
  508. string shortName = folderName.Substring(lastSlashIndex + 1);
  509. RecipeNameInputDlg dlg =
  510. new RecipeNameInputDlg(
  511. string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameFolderInfo"].ToString(),
  512. shortName), Application.Current.Resources["GlobalLableMsgInputFolderName"].ToString())
  513. {
  514. Owner = Application.Current.MainWindow
  515. };
  516. if (dlg.ShowDialog() == true)
  517. {
  518. string dir = lastSlashIndex == -1 ? "" : folderName.Substring(0, lastSlashIndex);
  519. if (!_viewModel.RenameFolder(folderName, dir + "\\" + dlg.InputText))
  520. {
  521. MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeRenameFolderFailed"].ToString(),
  522. Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK,
  523. MessageBoxImage.Warning);
  524. }
  525. else
  526. {
  527. UpdateRecipeFileList();
  528. }
  529. }
  530. }
  531. catch (Exception ex)
  532. {
  533. LOG.Write(ex);
  534. MessageBox.Show(
  535. string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameFolderException"].ToString(),
  536. ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(),
  537. MessageBoxButton.OK, MessageBoxImage.Error);
  538. }
  539. }
  540. /// <summary>
  541. /// import external recipe file into selected folder
  542. /// </summary>
  543. /// <param name="sender"></param>
  544. /// <param name="e"></param>
  545. void menuItem_MouseClick_ImportRecipe(object sender, RoutedEventArgs e)
  546. {
  547. try
  548. {
  549. MenuItem mit = sender as MenuItem;
  550. string folderName = mit.Tag as string;
  551. Microsoft.Win32.OpenFileDialog openDlg = new Microsoft.Win32.OpenFileDialog();
  552. openDlg.DefaultExt = ".rcp";
  553. openDlg.Filter = "Recipe file (.rcp)|*.rcp";
  554. openDlg.Multiselect = true;
  555. if (openDlg.ShowDialog() == true)
  556. {
  557. foreach(var openDlgFileName in openDlg.FileNames)
  558. {
  559. using (StreamReader srd = new StreamReader(openDlgFileName))
  560. {
  561. bool importFailed = false;
  562. string rcpContent = srd.ReadToEnd();
  563. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgRecipeImportInfo"].ToString(), Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  564. {
  565. Owner = Application.Current.MainWindow
  566. };
  567. dlg.InputText = System.IO.Path.GetFileNameWithoutExtension(openDlgFileName);
  568. if (dlg.ShowDialog() == true)
  569. {
  570. var recipeName = mit.Tag as string + "\\" + dlg.InputText;
  571. XmlDocument rcpDoc = new XmlDocument();
  572. rcpDoc.LoadXml(rcpContent);
  573. rcpDoc.DocumentElement.SetAttribute("RecipeVersion", _viewModel.RecipeVersion());
  574. if (!this._viewModel.SaveAsRecipe(recipeName, rcpDoc.OuterXml))
  575. {
  576. importFailed = true;
  577. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeImportFailed"].ToString(), recipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  578. }
  579. }
  580. srd.Close();
  581. if (!importFailed) File.Delete(openDlgFileName);
  582. UpdateRecipeFileList();
  583. SelectRecipe(mit.Tag as string + "\\" + dlg.InputText);
  584. }
  585. }
  586. }
  587. }
  588. catch (Exception ex)
  589. {
  590. LOG.Write(ex);
  591. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeImportException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  592. }
  593. }
  594. /// <summary>
  595. /// delete selected recipe files
  596. /// </summary>
  597. /// <param name="sender"></param>
  598. /// <param name="e"></param>
  599. void menuItem_MouseClick_DeleteRecipe(object sender, RoutedEventArgs e)
  600. {
  601. MenuItem mit = sender as MenuItem;
  602. string recipeName = mit.Tag as string;
  603. recipeName = PerformDeleteRecipe( recipeName);
  604. }
  605. private string PerformDeleteRecipe( string recipeName)
  606. {
  607. try
  608. {
  609. string origin = recipeName;
  610. int sepIndex = recipeName.LastIndexOf("\\");
  611. if (sepIndex < 0)
  612. sepIndex = 0;
  613. else
  614. sepIndex++;
  615. recipeName = recipeName.Substring(sepIndex);
  616. if (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteInfo"].ToString(), recipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(),
  617. MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  618. {
  619. this._viewModel.DeleteRecipe(origin);
  620. this.UpdateRecipeFileList();
  621. }
  622. }
  623. catch (Exception ex)
  624. {
  625. LOG.Write(ex);
  626. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  627. }
  628. return recipeName;
  629. }
  630. /// <summary>
  631. /// rename selected recipe files
  632. /// </summary>
  633. /// <param name="sender"></param>
  634. /// <param name="e"></param>
  635. void menuItem_MouseClick_RenameRecipe(object sender, RoutedEventArgs e)
  636. {
  637. MenuItem mit = sender as MenuItem;
  638. string recipeName = mit.Tag as string;
  639. PerformRenameRecipe(recipeName);
  640. }
  641. private void PerformRenameRecipe(string recipeName)
  642. {
  643. try
  644. {
  645. int lastSlashIndex = recipeName.LastIndexOf('\\');
  646. string shortName = recipeName.Substring(lastSlashIndex + 1);
  647. TreeViewItem rightClickedItem = this.treeViewRcpList.SelectedItem as TreeViewItem;
  648. RecipeNameInputDlg dlg = new RecipeNameInputDlg(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameInfo"].ToString(), shortName), Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  649. {
  650. InputText = shortName,
  651. Owner = Application.Current.MainWindow
  652. };
  653. if (dlg.ShowDialog() == true)
  654. {
  655. string newRecipeName = lastSlashIndex == -1 ? dlg.InputText : (recipeName.Substring(0, lastSlashIndex) + "\\" + dlg.InputText);
  656. var ret = this._viewModel.RenameRecipe(recipeName, newRecipeName);
  657. if (ret)
  658. {
  659. UpdateRecipeFileList();
  660. SelectRecipe(newRecipeName);
  661. }
  662. else
  663. {
  664. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameFailed"].ToString(), newRecipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  665. }
  666. }
  667. }
  668. catch (Exception ex)
  669. {
  670. LOG.Write(ex);
  671. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  672. }
  673. }
  674. /// <summary>
  675. /// export selected recipe files
  676. /// </summary>
  677. /// <param name="sender"></param>
  678. /// <param name="e"></param>
  679. void menuItem_MouseClick_ExportRecipe(object sender, RoutedEventArgs e)
  680. {
  681. try
  682. {
  683. MenuItem mit = sender as MenuItem;
  684. string recipeName = mit.Tag as string;
  685. var dlg = new Microsoft.Win32.SaveFileDialog();
  686. dlg.FileName = recipeName.Substring(recipeName.LastIndexOf('\\') + 1);
  687. dlg.DefaultExt = ".rcp";
  688. dlg.Filter = Application.Current.Resources["GlobalLableMsgRecipeExportFilter"].ToString();
  689. if (dlg.ShowDialog() == true)
  690. {
  691. using (StreamWriter sw = new StreamWriter(dlg.FileName))
  692. {
  693. sw.Write(this._viewModel.LoadRecipe(recipeName));
  694. sw.Close();
  695. }
  696. }
  697. }
  698. catch (Exception ex)
  699. {
  700. LOG.Write(ex);
  701. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeExportFailed"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  702. }
  703. }
  704. void menuItem_MouseClick_ExportExcel(object sender, RoutedEventArgs e)
  705. {
  706. MenuItem mit = sender as MenuItem;
  707. string recipeName = mit.Tag as string;
  708. var dlg = new Microsoft.Win32.SaveFileDialog();
  709. dlg.FileName = recipeName.Substring(recipeName.LastIndexOf('\\') + 1);
  710. dlg.DefaultExt = ".xls";
  711. dlg.Filter = Application.Current.Resources["GlobalLableMsgRecipeExportExcelFilter"].ToString();
  712. var ret = dlg.ShowDialog();
  713. if (!ret.HasValue || !ret.Value)
  714. return;
  715. string fileName = dlg.FileName;
  716. this._viewModel.CurrentRecipeName = recipeName;
  717. string xmlRecipeData = _viewModel.LoadRecipe(recipeName);
  718. XmlDocument doc = new XmlDocument();
  719. doc.LoadXml(_viewModel.RecipeFormat);
  720. Task.Factory.StartNew(() =>
  721. {
  722. try
  723. {
  724. if (File.Exists(fileName))
  725. File.Delete(fileName);
  726. Workbook workbook = new Workbook();
  727. Worksheet worksheet = new Worksheet("First Sheet");
  728. var catalogNodes = doc.SelectNodes("/TableRecipeFormat/Catalog");
  729. int groupRowIndex = 0;
  730. int stepRowIndex = 0;
  731. int catalogRowIndex = 0;
  732. List<string> excelRowHeaderName = new List<string>();
  733. foreach (XmlElement cNode in catalogNodes)
  734. {
  735. XmlNodeList sNodes = cNode.SelectNodes("Group/Step");
  736. string catalogHeader = cNode.Attributes["DisplayName"].Value;
  737. worksheet.Cells[catalogRowIndex + 1, 0] = new Cell(catalogHeader);
  738. catalogRowIndex += sNodes.Count;
  739. XmlNodeList gNodes = cNode.SelectNodes("Group");
  740. foreach (XmlElement gNode in gNodes)
  741. {
  742. XmlNodeList stepNodes = gNode.SelectNodes("Step");
  743. string groupHeader = gNode.Attributes["DisplayName"].Value;
  744. worksheet.Cells[groupRowIndex + 1, 1] = new Cell(groupHeader);
  745. groupRowIndex += stepNodes.Count;
  746. foreach (XmlElement nd in stepNodes)
  747. {
  748. string stepHeader = nd.Attributes["DisplayName"].Value;
  749. worksheet.Cells[stepRowIndex + 1, 2] = new Cell(stepHeader);
  750. excelRowHeaderName.Add(nd.Attributes["ControlName"].Value);
  751. stepRowIndex++;
  752. }
  753. }
  754. }
  755. XmlDocument rcp = new XmlDocument();
  756. rcp.LoadXml(xmlRecipeData);
  757. XmlNodeList stepNodeList = rcp.SelectNodes("/TableRecipeData/Step");
  758. for (int stepNo = 0; stepNo < stepNodeList.Count; stepNo++)
  759. {
  760. var dataDic = new Dictionary<string, string>();
  761. foreach (XmlAttribute att in (stepNodeList[stepNo] as XmlElement).Attributes)
  762. {
  763. dataDic.Add(att.Name, att.Value);
  764. }
  765. //fill sub node's attributes
  766. foreach (XmlElement subStep in stepNodeList[stepNo].ChildNodes)
  767. {
  768. foreach (XmlAttribute att in subStep.Attributes)
  769. {
  770. dataDic.Add(att.Name, att.Value);
  771. }
  772. foreach (XmlElement subsubStep in subStep.ChildNodes)
  773. {
  774. foreach (XmlAttribute att2 in subsubStep.Attributes)
  775. {
  776. dataDic.Add(att2.Name, att2.Value);
  777. }
  778. }
  779. }
  780. long length = dataDic.LongCount();
  781. worksheet.Cells[0, 3] = new Cell(recipeName);
  782. for (int rowIndex = 0; rowIndex < excelRowHeaderName.Count; rowIndex++)
  783. {
  784. worksheet.Cells.ColumnWidth[(ushort)(rowIndex + 1)] = 4000;
  785. string cellValueString = dataDic[excelRowHeaderName[rowIndex]];
  786. double cellValue;
  787. if (double.TryParse(cellValueString, out cellValue))
  788. {
  789. worksheet.Cells[rowIndex + 1, stepNo + 3] = new Cell(cellValue);
  790. }
  791. else
  792. {
  793. worksheet.Cells[rowIndex + 1, stepNo + 3] = new Cell(cellValueString);
  794. }
  795. }
  796. }
  797. workbook.Worksheets.Add(worksheet);
  798. workbook.Save(fileName);
  799. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeExportExcelOk"].ToString(), recipeName, fileName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Information);
  800. }
  801. catch (Exception ex)
  802. {
  803. LOG.Write(ex);
  804. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeExportExcelFailed"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  805. }
  806. });
  807. }
  808. private void ButtonNew(object sender, RoutedEventArgs e)
  809. {
  810. if (MenuPermission != 3) return;
  811. var focus = treeViewRcpList.SelectedItem;
  812. string folder = "";
  813. if (focus is TreeViewFileItem file)
  814. {
  815. var name = file.FileName;
  816. if (name.Contains("\\"))
  817. folder = name.Substring(0, name.LastIndexOf('\\'));
  818. }
  819. else if (focus is TreeViewFolderItem folderItem)
  820. {
  821. folder = folderItem.FolderName;
  822. }
  823. PerformCreateRecipe(folder);
  824. }
  825. private void ButtonRename(object sender, RoutedEventArgs e)
  826. {
  827. if (MenuPermission != 3) return;
  828. var focus = treeViewRcpList.SelectedItem;
  829. if (focus is TreeViewFileItem file)
  830. {
  831. var name = file.FileName;
  832. PerformRenameRecipe(file.FileName);
  833. }
  834. else if (focus is TreeViewFolderItem folderItem)
  835. {
  836. PerformRenameFolder(folderItem.FolderName);
  837. }
  838. }
  839. private void ButtonDelete(object sender, RoutedEventArgs e)
  840. {
  841. if (MenuPermission != 3) return;
  842. var focus = treeViewRcpList.SelectedItem;
  843. if (focus is TreeViewFileItem file)
  844. {
  845. var name = file.FileName;
  846. PerformDeleteRecipe(file.FileName);
  847. }
  848. else if (focus is TreeViewFolderItem folderItem)
  849. {
  850. PerformDeleteFolder(folderItem.FolderName);
  851. }
  852. }
  853. private void PerformCreateRecipe(string folderName)
  854. {
  855. try
  856. {
  857. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  858. {
  859. Owner = Application.Current.MainWindow
  860. };
  861. if (dlg.ShowDialog() == true)
  862. {
  863. var recipeName = folderName + "\\" + dlg.InputText;
  864. if (!this._viewModel.SaveAsRecipe(recipeName, this._viewModel.RecipeTemplate))
  865. {
  866. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateFailed"].ToString(), recipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  867. }
  868. else
  869. {
  870. UpdateRecipeFileList();
  871. SelectRecipe(folderName + "\\" + dlg.InputText);
  872. }
  873. }
  874. }
  875. catch (Exception ex)
  876. {
  877. LOG.Write(ex);
  878. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  879. }
  880. }
  881. private void TreeViewRcpList_DragEnter(object sender, DragEventArgs e)
  882. {
  883. try
  884. {
  885. if (GetAncestor<TreeViewFileItem>((DependencyObject)e.Source) != null)
  886. {
  887. if (GetAncestor<TreeViewFileItem>((DependencyObject)e.Source) == GetAncestor<TreeViewFileItem>((DependencyObject)OriginalItem)
  888. || VisualTreeHelper.GetParent(GetAncestor<TreeViewFileItem>((DependencyObject)e.Source)) == VisualTreeHelper.GetParent(GetAncestor<TreeViewFileItem>((DependencyObject)OriginalItem)))
  889. e.Effects = DragDropEffects.None;
  890. }
  891. else if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) != null)
  892. {
  893. if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) == GetAncestor<TreeViewFolderItem>((DependencyObject)OriginalItem))
  894. e.Effects = DragDropEffects.None;
  895. }
  896. else if (e.Source is TreeView)
  897. {
  898. e.Effects = DragDropEffects.None;
  899. }
  900. e.Handled = true;
  901. }
  902. catch (Exception ex)
  903. {
  904. LOG.Write(ex);
  905. }
  906. }
  907. private void TreeViewRcpList_Drop(object sender, DragEventArgs e)
  908. {
  909. try
  910. {
  911. TreeViewItem itemRemoved = treeViewRcpList.SelectedItem as TreeViewItem;
  912. #region
  913. //foreach (TreeViewItem item in treeViewRcpList.Items)
  914. //{
  915. // if (itemRemoved.Equals(item))
  916. // {
  917. // treeViewRcpList.Items.Remove(itemRemoved);
  918. // break;
  919. // }
  920. // if (item.Items.Count > 0)
  921. // {
  922. // foreach (TreeViewItem subItem in item.Items)
  923. // {
  924. // if (itemRemoved.Equals(subItem))
  925. // {
  926. // item.Items.Remove(itemRemoved);
  927. // break;
  928. // }
  929. // }
  930. // }
  931. //}
  932. //(getTreeViewFolderItem as TreeViewFolderItem).Items.Add(itemRemoved);
  933. #endregion
  934. if (GetAncestor<TreeViewFileItem>((DependencyObject)e.Source) != null)
  935. {
  936. if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) != null)
  937. {
  938. TreeViewFolderItem getTreeViewFolderItem = GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source);
  939. tragetFilePath = getTreeViewFolderItem.FolderName;
  940. GetAncestor<TreeView>(getTreeViewFolderItem);
  941. _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName, tragetFilePath);
  942. }
  943. else
  944. {
  945. tragetFilePath = "";
  946. GetAncestor<TreeView>((DependencyObject)e.Source);
  947. _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName, tragetFilePath);
  948. }
  949. }
  950. else if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) != null)
  951. {
  952. TreeViewFolderItem getTreeViewFolderItem = GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source);
  953. tragetFilePath = getTreeViewFolderItem.FolderName;
  954. GetAncestor<TreeView>(getTreeViewFolderItem);
  955. _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName, tragetFilePath);
  956. }
  957. UpdateRecipeFileList();
  958. SelectRecipe(tragetFilePath + "\\" + System.IO.Path.GetFileNameWithoutExtension(((TreeViewFileItem)itemRemoved).FileName));
  959. }
  960. catch (Exception ex)
  961. {
  962. LOG.Write(ex);
  963. }
  964. }
  965. private void TreeViewRcpList_MouseDown(object sender, MouseButtonEventArgs e)
  966. {
  967. if (e.ChangedButton == MouseButton.Left)
  968. {
  969. _lastMouseDown = e.GetPosition(treeViewRcpList);
  970. }
  971. }
  972. private void TreeViewRcpList_MouseMove(object sender, MouseEventArgs e)
  973. {
  974. try
  975. {
  976. if (e.LeftButton == MouseButtonState.Pressed)
  977. {
  978. Point currentPosition = e.GetPosition(treeViewRcpList);
  979. OriginalItem = e.OriginalSource;
  980. if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > 2.0) || (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > 2.0))
  981. {
  982. if ((treeViewRcpList.SelectedItem as TreeViewItem) != null)
  983. {
  984. //DragDropEffects finalDropEffect = DragDrop.DoDragDrop(treeViewRcpList, treeViewRcpList.SelectedItem, DragDropEffects.Move);
  985. DragDropEffects finalDropEffect = DragDrop.DoDragDrop(treeViewRcpList.SelectedItem as TreeViewItem, sender, DragDropEffects.Move);
  986. }
  987. }
  988. }
  989. }
  990. catch (Exception ex)
  991. {
  992. LOG.Write(ex);
  993. }
  994. }
  995. /// <summary>
  996. /// 子页面获取父类窗体
  997. /// </summary>
  998. /// <typeparam name="T"></typeparam>
  999. /// <param name="reference"></param>
  1000. /// <returns></returns>
  1001. public T GetAncestor<T>(DependencyObject reference) where T : DependencyObject
  1002. {
  1003. DependencyObject parent = VisualTreeHelper.GetParent(reference);
  1004. while (!(parent is T) && parent != null)
  1005. {
  1006. parent = VisualTreeHelper.GetParent(parent);
  1007. if (parent is TreeViewFolderItem)
  1008. tragetFilePath = tragetFilePath.Insert(0, $"{((TreeViewFolderItem)parent).FolderName}\\");
  1009. }
  1010. if (parent != null)
  1011. return (T)parent;
  1012. else
  1013. return null;
  1014. }//在子页面中调用GetAncestor<MainWindow>(this);
  1015. }
  1016. }