using Avalonia.Collections; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Data; using Avalonia.Markup.Xaml.MarkupExtensions; using Avalonia.Media; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace ShakerApp.ViewModels { public class MenuItemViewModel:ObservableObject { private string header = string.Empty; private string iconKey = string.Empty; private bool isEnabled = true; private bool isVisible = true; private bool isSeparator = false; private bool iconVisibile = true; public bool IsSeparator { get => isSeparator; set =>SetProperty(ref isSeparator , value); } public string Header { get => header; set =>SetProperty(ref header , value); } public bool IconVisibile { get => iconVisibile; set =>SetProperty(ref iconVisibile , value); } public string IconKey { get => iconKey; set =>SetProperty(ref iconKey , value); } public bool IsEnabled { get => isEnabled; set =>SetProperty(ref isEnabled , value); } public bool IsVisible { get => isVisible; set =>SetProperty(ref isVisible , value); } [AllowNull] public MenuItem MenuItem { get; private set; } public AvaloniaList Items { get; } = new AvaloniaList(); public Separator GetSeparator() { var separator = new Separator(); separator.DataContext = this; separator.Bind(Separator.IsVisibleProperty, new Binding() { Path = nameof(IsVisible), Mode = BindingMode.TwoWay, }); separator.Bind(Separator.IsEnabledProperty, new Binding() { Path = nameof(IsEnabled), Mode = BindingMode.TwoWay, }); return separator; } public MenuItemViewModel(params MenuItemViewModel[] menus) { if (menus == null) return; Items.AddRange(menus); } public MenuItem ConverterToMenuItem() { var menuItem = new Avalonia.Controls.MenuItem(); this.MenuItem = menuItem; menuItem.DataContext = this; menuItem.Bind(Avalonia.Controls.MenuItem.HeaderProperty, new DynamicResourceExtension(Header)); if (!string.IsNullOrEmpty(IconKey)) { var pathicon = new PathIcon(); pathicon.Bind(PathIcon.DataProperty, new DynamicResourceExtension(IconKey)); pathicon.Bind(PathIcon.IsVisibleProperty, new Binding() { Path = nameof(IconVisibile), Mode = BindingMode.TwoWay, }); menuItem.Icon = pathicon; } menuItem.Bind(MenuItem.CommandProperty, new Binding() { Path = nameof(MenuViewModel.Command), Mode = BindingMode.TwoWay, Source = MenuViewModel.Instance, }); menuItem.CommandParameter = this; menuItem.Bind(MenuItem.IsEnabledProperty, new Binding() { Path = nameof(IsEnabled), Mode = BindingMode.TwoWay, }); menuItem.Bind(MenuItem.IsVisibleProperty, new Binding() { Path = nameof(IsVisible), Mode = BindingMode.TwoWay, }); if(Items.Count>0) { for(int i=0;i