Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CommandMenuItem.cs « Custom « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fd1039ab0ed17c1765ac646fd144645a5214c9ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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);
		}
	}
}