diff options
author | Dominique Louis <savagesoftware@gmail.com> | 2018-01-24 17:33:33 +0300 |
---|---|---|
committer | Dominique Louis <savagesoftware@gmail.com> | 2018-04-20 20:53:27 +0300 |
commit | ebdb05abac8cd0fe1ffc995c7ad32b4f34cce6cb (patch) | |
tree | 986296f78f562510b5cdab5c65edf1656ee04a20 /Xamarin.PropertyEditing.Mac/Controls/Custom/CommandMenuItem.cs | |
parent | 57f5f4430942723a68ebdc8c78f83d0cd948b79d (diff) |
Hooking up PropertyButton to ResetCommand.
Diffstat (limited to 'Xamarin.PropertyEditing.Mac/Controls/Custom/CommandMenuItem.cs')
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/Custom/CommandMenuItem.cs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/CommandMenuItem.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/CommandMenuItem.cs new file mode 100644 index 0000000..1cdc73e --- /dev/null +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/CommandMenuItem.cs @@ -0,0 +1,56 @@ +using System; +using System.Windows.Input; +using AppKit; + +namespace Xamarin.PropertyEditing.Mac +{ + public class CommandMenuItem : NSMenuItem + { + ICommand command; + + public ICommand Command { + get { return command; } + set { + if (this.command != null) + this.command.CanExecuteChanged -= CanExecuteChanged; + + this.command = value; + this.command.CanExecuteChanged += CanExecuteChanged; + } + } + + public CommandMenuItem (string title) : base (title) + { + HookUpCommandEvents (); + } + + public CommandMenuItem (string title, ICommand command) : this (title) + { + this.Command = command; + } + + private void HookUpCommandEvents () + { + Activated += (object sender, EventArgs e) => { + if (this.command != null) + this.command.Execute (null); + }; + + ValidateMenuItem = ValidatePropertyMenuItem; + } + + private bool ValidatePropertyMenuItem (NSMenuItem menuItem) + { + if (this.command != null) + return this.command.CanExecute (null); + + return false; + } + + private void CanExecuteChanged (object sender, EventArgs e) + { + if(sender is ICommand cmd) + this.Enabled = cmd.CanExecute (null); + } + } +} |