From 758d80d0dacea2d4d34ee6bccca5577626f0cfdf Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 1 Jan 2024 21:36:49 +0100 Subject: [PATCH] Add ToggleableCommand --- ILSpy/Commands/SimpleCommand.cs | 36 +++++++++++++++++++++++++++++++++ ILSpy/MainWindow.xaml.cs | 7 +++++++ 2 files changed, 43 insertions(+) diff --git a/ILSpy/Commands/SimpleCommand.cs b/ILSpy/Commands/SimpleCommand.cs index 8822482e7..67c7f517e 100644 --- a/ILSpy/Commands/SimpleCommand.cs +++ b/ILSpy/Commands/SimpleCommand.cs @@ -17,6 +17,7 @@ // DEALINGS IN THE SOFTWARE. using System; +using System.ComponentModel; using System.Windows.Input; namespace ICSharpCode.ILSpy @@ -35,4 +36,39 @@ namespace ICSharpCode.ILSpy return true; } } + + public abstract class ToggleableCommand : ICommand, INotifyPropertyChanged + { + private bool isChecked; + + public event EventHandler CanExecuteChanged { + add { CommandManager.RequerySuggested += value; } + remove { CommandManager.RequerySuggested -= value; } + } + + public event PropertyChangedEventHandler PropertyChanged; + + void ICommand.Execute(object parameter) + { + IsChecked = Execute(parameter); + } + + public bool IsChecked { + get => isChecked; + set { + if (isChecked != value) + { + isChecked = value; + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsChecked))); + } + } + } + + public abstract bool Execute(object parameter); + + public virtual bool CanExecute(object parameter) + { + return true; + } + } } diff --git a/ILSpy/MainWindow.xaml.cs b/ILSpy/MainWindow.xaml.cs index 6f31ea9e5..3b8a62cab 100644 --- a/ILSpy/MainWindow.xaml.cs +++ b/ILSpy/MainWindow.xaml.cs @@ -29,6 +29,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; +using System.Windows.Data; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; @@ -299,6 +300,12 @@ namespace ICSharpCode.ILSpy } menuItem.IsEnabled = entry.Metadata.IsEnabled; + if (entry.Value is ToggleableCommand toggle) + { + menuItem.IsCheckable = true; + menuItem.SetBinding(MenuItem.IsCheckedProperty, new Binding("IsChecked") { Source = entry.Value, Mode = BindingMode.OneWay }); + } + menuItem.InputGestureText = entry.Metadata.InputGestureText; parentMenuItem.Items.Add(menuItem); }