123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 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 = null;
- 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 ICommand Command => new RelayCommand<string?>((p) => Action?.Invoke(p));
- [AllowNull]
- public Action<string?> Action { get; set; }
- public bool IsEnabled { get => isEnabled; set =>SetProperty(ref isEnabled , value); }
- public bool IsVisible { get => isVisible; set =>SetProperty(ref isVisible , value); }
- public AvaloniaList<MenuItemViewModel> Items { get; } = new AvaloniaList<MenuItemViewModel>();
- public Separator GetSeparator()
- {
- var separator = new Separator();
- separator.DataContext = this;
- separator.Bind(Separator.IsVisibleProperty, new Binding()
- {
- Path = nameof(IsSeparator),
- Mode = BindingMode.TwoWay,
- });
- separator.Bind(Separator.IsEnabledProperty, new Binding()
- {
- Path = nameof(IsEnabled),
- Mode = BindingMode.TwoWay,
- });
- return separator;
- }
- public MenuItem ConverterToMenuItem()
- {
- var menuItem = new Avalonia.Controls.MenuItem();
- menuItem.DataContext = this;
- menuItem.Bind(Avalonia.Controls.MenuItem.HeaderProperty, new DynamicResourceExtension(Header));
- menuItem.Bind(Avalonia.Controls.MenuItem.CommandParameterProperty, new Binding()
- {
- Path=nameof(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(Command),
- Mode = BindingMode.TwoWay,
- });
- 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<Items.Count;i++)
- {
- if (Items[i].IsSeparator) menuItem.Items.Add(Items[i].GetSeparator());
- else menuItem.Items.Add(Items[i].ConverterToMenuItem());
- }
- }
- return menuItem;
- }
- }
- }
|