Browse Source

Add ToggleableCommand

natural-type-lambdas-methods
Siegfried Pammer 2 years ago
parent
commit
758d80d0da
  1. 36
      ILSpy/Commands/SimpleCommand.cs
  2. 7
      ILSpy/MainWindow.xaml.cs

36
ILSpy/Commands/SimpleCommand.cs

@ -17,6 +17,7 @@ @@ -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 @@ -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;
}
}
}

7
ILSpy/MainWindow.xaml.cs

@ -29,6 +29,7 @@ using System.Text; @@ -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 @@ -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);
}

Loading…
Cancel
Save