MenuItemViewModel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Primitives;
  4. using Avalonia.Data;
  5. using Avalonia.Markup.Xaml.MarkupExtensions;
  6. using Avalonia.Media;
  7. using CommunityToolkit.Mvvm.ComponentModel;
  8. using CommunityToolkit.Mvvm.Input;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics.CodeAnalysis;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Input;
  16. namespace ShakerApp.ViewModels
  17. {
  18. public class MenuItemViewModel:ObservableObject
  19. {
  20. private string header = string.Empty;
  21. private string iconKey = string.Empty;
  22. private bool isEnabled = true;
  23. private bool isVisible = true;
  24. private bool isSeparator = false;
  25. private bool iconVisibile = true;
  26. public bool IsSeparator { get => isSeparator; set =>SetProperty(ref isSeparator , value); }
  27. public string Header { get => header; set =>SetProperty(ref header , value); }
  28. public bool IconVisibile { get => iconVisibile; set =>SetProperty(ref iconVisibile , value); }
  29. public string IconKey { get => iconKey; set =>SetProperty(ref iconKey , value); }
  30. public bool IsEnabled { get => isEnabled; set =>SetProperty(ref isEnabled , value); }
  31. public bool IsVisible { get => isVisible; set =>SetProperty(ref isVisible , value); }
  32. [AllowNull]
  33. public MenuItem MenuItem { get; private set; }
  34. public AvaloniaList<MenuItemViewModel> Items { get; } = new AvaloniaList<MenuItemViewModel>();
  35. public Separator GetSeparator()
  36. {
  37. var separator = new Separator();
  38. separator.DataContext = this;
  39. separator.Bind(Separator.IsVisibleProperty, new Binding()
  40. {
  41. Path = nameof(IsVisible),
  42. Mode = BindingMode.TwoWay,
  43. });
  44. separator.Bind(Separator.IsEnabledProperty, new Binding()
  45. {
  46. Path = nameof(IsEnabled),
  47. Mode = BindingMode.TwoWay,
  48. });
  49. return separator;
  50. }
  51. public MenuItemViewModel(params MenuItemViewModel[] menus)
  52. {
  53. if (menus == null) return;
  54. Items.AddRange(menus);
  55. }
  56. public MenuItem ConverterToMenuItem()
  57. {
  58. var menuItem = new Avalonia.Controls.MenuItem();
  59. this.MenuItem = menuItem;
  60. menuItem.DataContext = this;
  61. menuItem.Bind(Avalonia.Controls.MenuItem.HeaderProperty, new DynamicResourceExtension(Header));
  62. if (!string.IsNullOrEmpty(IconKey))
  63. {
  64. var pathicon = new PathIcon();
  65. pathicon.Bind(PathIcon.DataProperty, new DynamicResourceExtension(IconKey));
  66. pathicon.Bind(PathIcon.IsVisibleProperty, new Binding()
  67. {
  68. Path = nameof(IconVisibile),
  69. Mode = BindingMode.TwoWay,
  70. });
  71. menuItem.Icon = pathicon;
  72. }
  73. menuItem.Bind(MenuItem.CommandProperty, new Binding()
  74. {
  75. Path = nameof(MenuViewModel.Command),
  76. Mode = BindingMode.TwoWay,
  77. Source = MenuViewModel.Instance,
  78. });
  79. menuItem.CommandParameter = this;
  80. menuItem.Bind(MenuItem.IsEnabledProperty, new Binding()
  81. {
  82. Path = nameof(IsEnabled),
  83. Mode = BindingMode.TwoWay,
  84. });
  85. menuItem.Bind(MenuItem.IsVisibleProperty, new Binding()
  86. {
  87. Path = nameof(IsVisible),
  88. Mode = BindingMode.TwoWay,
  89. });
  90. if(Items.Count>0)
  91. {
  92. for(int i=0;i<Items.Count;i++)
  93. {
  94. if (Items[i].IsSeparator) menuItem.Items.Add(Items[i].GetSeparator());
  95. else menuItem.Items.Add(Items[i].ConverterToMenuItem());
  96. }
  97. }
  98. return menuItem;
  99. }
  100. }
  101. }