diff options
author | Eric Maupin <ermaup@microsoft.com> | 2019-06-12 18:22:19 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-12 18:22:19 +0300 |
commit | fd8c56278e746c9bba04a66e78bfe33d1d2e0307 (patch) | |
tree | 699e9016826e979bc8bba42d293d595bbf49042d /Xamarin.PropertyEditing.Mac | |
parent | 870efb06c81e128b338b691e4369bf4c4791e4ed (diff) | |
parent | 45706274e35941d4e599b6ae4d48a2dd4ecf561e (diff) |
Merge pull request #601 from xamarin/dominique-CommandButton
[Mac] NSButton that handles ICommands. Needed for Binding Dialog and …
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; } } |