diff options
Diffstat (limited to 'Xamarin.PropertyEditing.Mac')
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/Custom/CommandButton.cs | 40 | ||||
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/Custom/CommandMenuItem.cs | 4 |
2 files changed, 43 insertions, 1 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/CommandButton.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/CommandButton.cs new file mode 100644 index 0000000..4904bd8 --- /dev/null +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/CommandButton.cs @@ -0,0 +1,40 @@ +using System; +using System.Windows.Input; +using AppKit; + +namespace Xamarin.PropertyEditing.Mac.Controls.Custom +{ + internal class CommandButton : NSButton + { + private ICommand command; + + public ICommand Command + { + get { return this.command; } + set + { + if (this.command != null) + this.command.CanExecuteChanged -= CanExecuteChanged; + + this.command = value; + + if (this.command != null) + this.command.CanExecuteChanged += CanExecuteChanged; + } + } + + public CommandButton () + { + Activated += (object sender, EventArgs e) => { + var button = (CommandButton)sender; + button.command?.Execute (null); + }; + } + + private void CanExecuteChanged (object sender, EventArgs e) + { + if (sender is ICommand cmd) + this.Enabled = cmd.CanExecute (null); + } + } +} diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/CommandMenuItem.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/CommandMenuItem.cs index c945a80..1a84424 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/CommandMenuItem.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/CommandMenuItem.cs @@ -15,7 +15,9 @@ namespace Xamarin.PropertyEditing.Mac this.command.CanExecuteChanged -= CanExecuteChanged; this.command = value; - this.command.CanExecuteChanged += CanExecuteChanged; + + if (this.command != null) + this.command.CanExecuteChanged += CanExecuteChanged; } } |