MenuItemViewModel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 = null;
  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 ICommand Command => new RelayCommand<string?>((p) => Action?.Invoke(p));
  31. [AllowNull]
  32. public Action<string?> Action { get; set; }
  33. public bool IsEnabled { get => isEnabled; set =>SetProperty(ref isEnabled , value); }
  34. public bool IsVisible { get => isVisible; set =>SetProperty(ref isVisible , value); }
  35. public AvaloniaList<MenuItemViewModel> Items { get; } = new AvaloniaList<MenuItemViewModel>();
  36. public Separator GetSeparator()
  37. {
  38. var separator = new Separator();
  39. separator.DataContext = this;
  40. separator.Bind(Separator.IsVisibleProperty, new Binding()
  41. {
  42. Path = nameof(IsSeparator),
  43. Mode = BindingMode.TwoWay,
  44. });
  45. separator.Bind(Separator.IsEnabledProperty, new Binding()
  46. {
  47. Path = nameof(IsEnabled),
  48. Mode = BindingMode.TwoWay,
  49. });
  50. return separator;
  51. }
  52. public MenuItem ConverterToMenuItem()
  53. {
  54. var menuItem = new Avalonia.Controls.MenuItem();
  55. menuItem.DataContext = this;
  56. menuItem.Bind(Avalonia.Controls.MenuItem.HeaderProperty, new DynamicResourceExtension(Header));
  57. menuItem.Bind(Avalonia.Controls.MenuItem.CommandParameterProperty, new Binding()
  58. {
  59. Path=nameof(Header),
  60. });
  61. if (!string.IsNullOrEmpty(IconKey))
  62. {
  63. var pathicon = new PathIcon();
  64. pathicon.Bind(PathIcon.DataProperty, new DynamicResourceExtension(IconKey));
  65. pathicon.Bind(PathIcon.IsVisibleProperty, new Binding()
  66. {
  67. Path = nameof(IconVisibile),
  68. Mode = BindingMode.TwoWay,
  69. });
  70. menuItem.Icon = pathicon;
  71. }
  72. menuItem.Bind(MenuItem.CommandProperty, new Binding()
  73. {
  74. Path = nameof(Command),
  75. Mode = BindingMode.TwoWay,
  76. });
  77. menuItem.Bind(MenuItem.IsEnabledProperty, new Binding()
  78. {
  79. Path = nameof(IsEnabled),
  80. Mode = BindingMode.TwoWay,
  81. });
  82. menuItem.Bind(MenuItem.IsVisibleProperty, new Binding()
  83. {
  84. Path = nameof(IsVisible),
  85. Mode = BindingMode.TwoWay,
  86. });
  87. if(Items.Count>0)
  88. {
  89. for(int i=0;i<Items.Count;i++)
  90. {
  91. if (Items[i].IsSeparator) menuItem.Items.Add(Items[i].GetSeparator());
  92. else menuItem.Items.Add(Items[i].ConverterToMenuItem());
  93. }
  94. }
  95. return menuItem;
  96. }
  97. }
  98. }