QuickToolsContentViewModel.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. using ComPDFKitViewer.PdfViewer;
  2. using Microsoft.Office.Core;
  3. using PDF_Master.CustomControl;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Model;
  6. using PDF_Master.Model.Dialog.ConverterDialogs;
  7. using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  8. using PDF_Master.Model.Dialog.ToolsDialogs;
  9. using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs;
  10. using PDF_Master.Model.PDFTool;
  11. using PDF_Master.Properties;
  12. using PDF_Master.Views;
  13. using PDFSettings;
  14. using Prism.Commands;
  15. using Prism.Mvvm;
  16. using Prism.Regions;
  17. using Prism.Services.Dialogs;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Text;
  22. using System.Threading.Tasks;
  23. using System.Windows.Controls;
  24. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  25. namespace PDF_Master.ViewModels.HomePanel.PDFTools
  26. {
  27. public class QuickToolsContentViewModel : BindableBase
  28. {
  29. #region 文案
  30. private string T_title;
  31. public string T_Title
  32. {
  33. get { return T_title; }
  34. set
  35. {
  36. SetProperty(ref T_title, value);
  37. }
  38. }
  39. private void InitString()
  40. {
  41. T_Title = App.HomePageLoader.GetString("HomeTool_Title");
  42. }
  43. #endregion
  44. #region 属性
  45. /// <summary>
  46. /// 扩展/收缩 默认展开
  47. /// </summary>
  48. private bool _isExpendTools = true;
  49. public bool IsExpendTools
  50. {
  51. get { return _isExpendTools; }
  52. set { SetProperty(ref _isExpendTools, value); }
  53. }
  54. private string regionName;
  55. public string ToolRegionName
  56. {
  57. get { return regionName; }
  58. set { SetProperty(ref regionName, value); }
  59. }
  60. #endregion
  61. #region Command and Event
  62. public IDialogService dialogs;
  63. public DelegateCommand<ToolItem> QuickToolsCommand { get; set; }
  64. public DelegateCommand<object> OpenMenuCommand { get; set; }
  65. public DelegateCommand ExpendCommand { get; set; }
  66. public DelegateCommand ShowToolCommand { get; set; }
  67. public IRegionManager toolregion;
  68. public event EventHandler<bool> ExpendToolsHanlder;
  69. #endregion
  70. public QuickToolsContentViewModel(IDialogService dialogService, IRegionManager regionManager)
  71. {
  72. toolregion = regionManager;
  73. ToolRegionName = RegionNames.ToolRegionName;
  74. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  75. {
  76. toolregion.RequestNavigate(ToolRegionName, "Guid");
  77. }));
  78. dialogs = dialogService;
  79. InitVariable();
  80. InitCommand();
  81. InitString();
  82. }
  83. #region 初始化和绑定
  84. private void InitVariable()
  85. {
  86. }
  87. private void InitCommand()
  88. {
  89. QuickToolsCommand = new DelegateCommand<ToolItem>(QuickTools_Click);
  90. OpenMenuCommand = new DelegateCommand<object>(OpenMenu);
  91. ExpendCommand = new DelegateCommand(Expend);
  92. ShowToolCommand = new DelegateCommand(ShowTool);
  93. }
  94. #endregion
  95. private void ShowTool()
  96. {
  97. toolregion.RequestNavigate(ToolRegionName, "Tools");
  98. }
  99. public void QuickTools_Click(ToolItem toolItem)
  100. {
  101. /*
  102. *设置这个对话框的起始保存路径
  103. */
  104. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  105. dlg.Multiselect = false;
  106. dlg.Filter = "PDF|*.pdf;*.PDF;";
  107. //MVP原规划的批量工具 暂时先注释掉
  108. //if (toolItem.FnType == PDFFnType.Compress || toolItem.FnType == PDFFnType.Security||
  109. // toolItem.FnType ==PDFFnType.BatchRemove||toolItem.FnType==PDFFnType.ConvertPDF||
  110. // toolItem.FnType==PDFFnType.Background|| toolItem.FnType == PDFFnType.HeaderFooter||
  111. // toolItem.FnType == PDFFnType.BatesNumbers|| toolItem.FnType == PDFFnType.WaterMark||
  112. // toolItem.FnType == PDFFnType.PDFToExcel|| toolItem.FnType == PDFFnType.PDFToImage||
  113. // toolItem.FnType == PDFFnType.PDFToPPT|| toolItem.FnType == PDFFnType.Batch ||
  114. // toolItem.FnType == PDFFnType.Merge)
  115. //{
  116. // //dlg.Multiselect = true;
  117. //}
  118. if (toolItem.FnType == PDFFnType.ImageToPDF)
  119. {
  120. dlg.Multiselect = true;
  121. dlg.Filter = "Picture|*.png;*.jpg;*bmp;*jpeg;*gif;*tiff;";
  122. }
  123. else if(toolItem.FnType == PDFFnType.Merge)
  124. {
  125. dlg.Multiselect = true;
  126. dlg.Filter = Properties.Resources.imageex.ToLower() + "|*";
  127. }
  128. //付费锁 图片转PDF不用加锁
  129. //删掉Settings.Default.UserDate.isInFreeUseTime == false,去掉试用策略
  130. if (toolItem.FnType != PDFFnType.ImageToPDF&&(!App.IsLogin) )
  131. {
  132. dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  133. return ;
  134. }
  135. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  136. {
  137. CPDFViewer viewer = new CPDFViewer();
  138. switch ((PDFFnType)toolItem.FnType)
  139. {
  140. case PDFFnType.Split:
  141. viewer.InitDocument(dlg.FileName);
  142. if (!CheckPassword(viewer)) {return;}
  143. DialogParameters splitvalue = new DialogParameters();
  144. splitvalue.Add(ParameterNames.PDFViewer, viewer);
  145. splitvalue.Add(ParameterNames.FilePath, dlg.FileName);
  146. dialogs.ShowDialog(DialogNames.HomePageSplitDialog, splitvalue, e => { });
  147. break;
  148. case PDFFnType.Extract:
  149. viewer.InitDocument(dlg.FileName);
  150. if (!CheckPassword(viewer)) {return;}
  151. DialogParameters extractvalue = new DialogParameters();
  152. extractvalue.Add(ParameterNames.PDFViewer, viewer);
  153. extractvalue.Add(ParameterNames.FilePath, dlg.FileName);
  154. dialogs.ShowDialog(DialogNames.HomePageExtractDialog, extractvalue, e => { });
  155. break;
  156. case PDFFnType.Insert:
  157. viewer.InitDocument(dlg.FileName);
  158. if (!CheckPassword(viewer)) {return;}
  159. DialogParameters insertvalue = new DialogParameters();
  160. insertvalue.Add(ParameterNames.PDFViewer, viewer);
  161. insertvalue.Add(ParameterNames.FilePath, dlg.FileName);
  162. dialogs.ShowDialog(DialogNames.HomePageInsertDialog, insertvalue, e => { });
  163. break;
  164. case PDFFnType.Compress:
  165. viewer.InitDocument(dlg.FileName);
  166. if (!CheckPassword(viewer)) { return; }
  167. DialogParameters compresspdf = new DialogParameters();
  168. compresspdf.Add(ParameterNames.PDFViewer, viewer);
  169. dialogs.ShowDialog(DialogNames.CompressDialog, compresspdf, e =>
  170. {
  171. CompressDialogModel compressDialogModel = new CompressDialogModel();
  172. compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e);
  173. // OnOpened((Prism.Services.Dialogs.DialogResult)e);
  174. });
  175. //批量
  176. //DialogParameters compresspdf = new DialogParameters();
  177. //compresspdf.Add(ParameterNames.BatchProcessing_Name, "1");
  178. //HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  179. //HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
  180. //compresspdf.Add(ParameterNames.FilePath, dlg.FileNames);
  181. //dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, compresspdf, e => { });
  182. break;
  183. case PDFFnType.ImageToPDF:
  184. DialogParameters picturetopdf = new DialogParameters();
  185. picturetopdf.Add(ParameterNames.FilePath, dlg.FileNames);
  186. dialogs.ShowDialog(DialogNames.HomePagePictureToPDFDialog, picturetopdf, e => { });
  187. break;
  188. case PDFFnType.Print:
  189. viewer.InitDocument(dlg.FileName);
  190. if (!CheckPassword(viewer)) {return;}
  191. DialogParameters printvalue = new DialogParameters();
  192. printvalue.Add(ParameterNames.PDFViewer, viewer);
  193. printvalue.Add(ParameterNames.FilePath, dlg.FileName);
  194. dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printvalue, e => { });
  195. try
  196. {
  197. dialogs.ShowDialog("MainWindow");
  198. }
  199. catch
  200. {
  201. }
  202. break;
  203. case PDFFnType.Security:
  204. //DialogParameters securitypdf = new DialogParameters();
  205. //securitypdf.Add(ParameterNames.BatchProcessing_Name, "2");
  206. //HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  207. //HomePageBatchProcessingDialogModel.BatchProcessingIndex = 2;
  208. //securitypdf.Add(ParameterNames.FilePath, dlg.FileNames);
  209. //dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, securitypdf, e => { });
  210. CPDFViewer cPDFViewer = new CPDFViewer();
  211. cPDFViewer.InitDocument(dlg.FileName);
  212. if (cPDFViewer.Document == null) {
  213. AlertsMessage alertsMessage = new AlertsMessage();
  214. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Main_OpenFileFailedWarning"), App.ServiceLoader.GetString("Text_ok"));
  215. return ;
  216. }
  217. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(cPDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  218. if (result.IsDiscryptied)
  219. {
  220. if (result.Password != null)
  221. {
  222. cPDFViewer.Document.UnlockWithPassword(result.Password);
  223. }
  224. ///TODO:
  225. ///此处填入需要执行的代码
  226. ///
  227. DialogParameters param = new DialogParameters();
  228. param.Add(ParameterNames.PDFViewer, cPDFViewer);
  229. dialogs.ShowDialog(DialogNames.SetPasswordDialog, param, e => {
  230. string FilePath;
  231. if (e.Parameters.TryGetValue("FilePath",out FilePath))
  232. {
  233. CommonHelper.ShowFileBrowser(FilePath);
  234. }
  235. });
  236. }
  237. break;
  238. case PDFFnType.ConvertPDF:
  239. //DialogParameters convertpdf = new DialogParameters();
  240. //convertpdf.Add(ParameterNames.BatchProcessing_Name, "0");
  241. //HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  242. //HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  243. //convertpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  244. //dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdf, e => { });
  245. break;
  246. case PDFFnType.BatchRemove:
  247. DialogParameters batchremovepdf = new DialogParameters();
  248. batchremovepdf.Add(ParameterNames.BatchProcessing_Name, "7");
  249. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  250. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 7;
  251. batchremovepdf.Add(ParameterNames.FilePath, dlg.FileNames);
  252. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batchremovepdf, e => { });
  253. break;
  254. case PDFFnType.HeaderFooter:
  255. DialogParameters headerFooterpdf = new DialogParameters();
  256. headerFooterpdf.Add(ParameterNames.BatchProcessing_Name, "5");
  257. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  258. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 5;
  259. headerFooterpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  260. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, headerFooterpdf, e => { });
  261. break;
  262. case PDFFnType.BatesNumbers:
  263. DialogParameters batesNumberspdf = new DialogParameters();
  264. batesNumberspdf.Add(ParameterNames.BatchProcessing_Name, "6");
  265. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  266. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 6;
  267. batesNumberspdf.Add(ParameterNames.FilePath, dlg.FileNames);
  268. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batesNumberspdf, e => { });
  269. break;
  270. case PDFFnType.Background:
  271. DialogParameters backgroundpdf = new DialogParameters();
  272. backgroundpdf.Add(ParameterNames.BatchProcessing_Name, "4");
  273. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  274. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 4;
  275. backgroundpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  276. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, backgroundpdf, e => { });
  277. break;
  278. case PDFFnType.WaterMark:
  279. DialogParameters watermarkpdf = new DialogParameters();
  280. watermarkpdf.Add(ParameterNames.BatchProcessing_Name, "3");
  281. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  282. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 3;
  283. watermarkpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  284. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, watermarkpdf, e => { });
  285. break;
  286. case PDFFnType.PDFToImage:
  287. viewer.InitDocument(dlg.FileName);
  288. if (!CheckPassword(viewer)) { return; }
  289. DialogParameters convertpdftoimage = new DialogParameters();
  290. convertpdftoimage.Add("PageRangeComboBoxCurrentPage", true);
  291. convertpdftoimage.Add(ParameterNames.PDFViewer, viewer);
  292. dialogs.ShowDialog(DialogNames.ConverterImgDialog, convertpdftoimage, e =>
  293. {
  294. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  295. converterDialogsModel.OnOpened((DialogResult)e);
  296. });
  297. //DialogParameters convertpdftoimage = new DialogParameters();
  298. //convertpdftoimage.Add(ParameterNames.BatchProcessing_Name, "0");
  299. //convertpdftoimage.Add("ConverterTypeIndex", 7);
  300. //HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  301. //HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  302. //convertpdftoimage.Add(ParameterNames.FilePath, dlg.FileNames);
  303. //dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoimage, e => { });
  304. break;
  305. case PDFFnType.PDFToExcel:
  306. viewer.InitDocument(dlg.FileName);
  307. if (!CheckPassword(viewer)) { return; }
  308. DialogParameters convertpdftoexcel = new DialogParameters();
  309. convertpdftoexcel.Add(ParameterNames.PDFViewer, viewer);
  310. convertpdftoexcel.Add("PageRangeComboBoxCurrentPage", true);
  311. dialogs.ShowDialog(DialogNames.ConverterExcelDialog, convertpdftoexcel, e =>
  312. {
  313. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  314. converterDialogsModel.OnOpened((DialogResult)e);
  315. });
  316. //DialogParameters convertpdftoexcel = new DialogParameters();
  317. //convertpdftoexcel.Add(ParameterNames.BatchProcessing_Name, "0");
  318. //convertpdftoexcel.Add("ConverterTypeIndex", 1);
  319. //HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  320. //HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  321. //convertpdftoexcel.Add(ParameterNames.FilePath, dlg.FileNames);
  322. //dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoexcel, e => { });
  323. break;
  324. case PDFFnType.PDFToPPT:
  325. viewer.InitDocument(dlg.FileName);
  326. if (!CheckPassword(viewer)) { return; }
  327. DialogParameters convertpdftoPPT = new DialogParameters();
  328. convertpdftoPPT.Add(ParameterNames.PDFViewer, viewer);
  329. convertpdftoPPT.Add("PageRangeComboBoxCurrentPage", true);
  330. dialogs.ShowDialog(DialogNames.ConverterPPTDialog, convertpdftoPPT, e =>
  331. {
  332. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  333. converterDialogsModel.OnOpened((DialogResult)e);
  334. });
  335. //批量处理
  336. //DialogParameters convertpdftoppt = new DialogParameters();
  337. //convertpdftoppt.Add(ParameterNames.BatchProcessing_Name, "0");
  338. //convertpdftoppt.Add("ConverterTypeIndex", 2);
  339. //HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  340. //HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  341. //convertpdftoppt.Add(ParameterNames.FilePath, dlg.FileNames);
  342. //dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoppt, e => { });
  343. break;
  344. case PDFFnType.PDFToWord:
  345. viewer.InitDocument(dlg.FileName);
  346. if (!CheckPassword(viewer)) { return; }
  347. DialogParameters convertpdftoword = new DialogParameters();
  348. convertpdftoword.Add(ParameterNames.PDFViewer, viewer);
  349. convertpdftoword.Add("PageRangeComboBoxCurrentPage", true);
  350. dialogs.ShowDialog(DialogNames.ConverterWordDialog, convertpdftoword, e =>
  351. {
  352. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  353. converterDialogsModel.OnOpened((DialogResult)e);
  354. });
  355. //批量处理
  356. //DialogParameters convertpdftoword = new DialogParameters();
  357. //convertpdftoword.Add(ParameterNames.BatchProcessing_Name, "0");
  358. //convertpdftoword.Add("ConverterTypeIndex", 0);
  359. //HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  360. //HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  361. //convertpdftoword.Add(ParameterNames.FilePath, dlg.FileNames);
  362. //dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoword, e => { });
  363. break;
  364. case PDFFnType.Batch:
  365. DialogParameters batcpdf = new DialogParameters();
  366. batcpdf.Add(ParameterNames.BatchProcessing_Name, "0");
  367. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  368. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  369. batcpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  370. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batcpdf, e => { });
  371. break;
  372. case PDFFnType.Merge:
  373. viewer.InitDocument(dlg.FileName);
  374. // if (!CheckPassword(viewer)) { return; }
  375. DialogParameters merge = new DialogParameters();
  376. merge.Add(ParameterNames.PDFViewer, viewer);
  377. merge.Add(ParameterNames.FilePath, dlg.FileNames);
  378. dialogs.ShowDialog(DialogNames.MergeDialog, merge, e =>
  379. {
  380. });
  381. break;
  382. }
  383. }
  384. }
  385. //
  386. //
  387. private bool CheckPassword(CPDFViewer viewer) {
  388. if (viewer.Document == null)
  389. {
  390. AlertsMessage alertsMessage = new AlertsMessage();
  391. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Main_OpenFileFailedWarning"), App.ServiceLoader.GetString("Text_ok"));
  392. return false;
  393. }
  394. if (viewer.Document.IsLocked)
  395. {
  396. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(viewer.Document, EnumPasswordKind.StatusOpenPassword, dialogs);
  397. if (result.IsDiscryptied)
  398. {
  399. if (result.Password != null)
  400. {
  401. if(viewer.Document.UnlockWithPassword(result.Password))
  402. {
  403. //if (viewer.Document.CheckOwnerPassword(result.Password))
  404. //{
  405. viewer.Tag = result.Password;
  406. //}
  407. }
  408. }
  409. ///TODO:
  410. ///此处填入需要执行的代码
  411. }
  412. if (result.IsDiscryptied == false)
  413. {
  414. //未成功解密文档时,释放Document对象,返回
  415. viewer.Document.Release();
  416. return false;
  417. }
  418. }
  419. return true;
  420. }
  421. private void Expend()
  422. {
  423. IsExpendTools = !IsExpendTools;
  424. ExpendToolsHanlder?.Invoke(null, IsExpendTools);
  425. }
  426. private void OpenMenu(object obj)
  427. {
  428. var menu = App.Current.FindResource("ExpendToolsContextMenu") as ContextMenu;
  429. var btn = obj as Button;
  430. if (menu != null && btn != null)
  431. {
  432. if(menu.Items.Count == 1)
  433. {
  434. var item = menu.Items[0] as MenuItem;
  435. if(_isExpendTools)
  436. {
  437. item.Header = "Collapse";
  438. }
  439. else
  440. {
  441. item.Header = "Expand";
  442. }
  443. item.Command = ExpendCommand;
  444. //btn.ContextMenu = menu;
  445. menu.PlacementTarget = btn;
  446. menu.IsOpen = true;
  447. }
  448. }
  449. }
  450. }
  451. }