PDFToolsContentViewModel.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Controls;
  9. namespace PDF_Office.ViewModels.HomePanel.PDFTools
  10. {
  11. public class PDFToolsContentViewModel : BindableBase
  12. {
  13. #region 属性
  14. /// <summary>
  15. /// 扩展/收缩
  16. /// </summary>
  17. private bool _isExpendTools = false;
  18. public bool IsExpendTools
  19. {
  20. get { return _isExpendTools; }
  21. set { SetProperty(ref _isExpendTools, value); }
  22. }
  23. #endregion
  24. #region Command and Event
  25. public DelegateCommand<object> OpenMenuCommand { get; set; }
  26. public DelegateCommand ExpendCommand { get; set; }
  27. public event EventHandler<bool> ExpendToolsHanlder;
  28. #endregion
  29. public PDFToolsContentViewModel()
  30. {
  31. InitCommand();
  32. }
  33. private void InitCommand()
  34. {
  35. OpenMenuCommand = new DelegateCommand<object>(OpenMenu);
  36. ExpendCommand = new DelegateCommand(Expend);
  37. }
  38. private void Expend()
  39. {
  40. IsExpendTools = !IsExpendTools;
  41. ExpendToolsHanlder?.Invoke(null, IsExpendTools);
  42. }
  43. private void OpenMenu(object obj)
  44. {
  45. var menu = App.Current.FindResource("ExpendToolsContextMenu") as ContextMenu;
  46. var btn = obj as Button;
  47. if (menu != null && btn != null)
  48. {
  49. if (menu.Items.Count == 1)
  50. {
  51. var item = menu.Items[0] as MenuItem;
  52. if (_isExpendTools)
  53. {
  54. item.Header = "收缩";
  55. }
  56. else
  57. {
  58. item.Header = "展开";
  59. }
  60. item.Command = ExpendCommand;
  61. //btn.ContextMenu = menu;
  62. menu.PlacementTarget = btn;
  63. menu.IsOpen = true;
  64. }
  65. }
  66. }
  67. }
  68. }