diff options
author | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 13:02:25 -0700 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 16:13:41 -0700 |
commit | 17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch) | |
tree | b5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Core/MenuItem.cs | |
download | xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2 xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip |
Initial import
Diffstat (limited to 'Xamarin.Forms.Core/MenuItem.cs')
-rw-r--r-- | Xamarin.Forms.Core/MenuItem.cs | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/MenuItem.cs b/Xamarin.Forms.Core/MenuItem.cs new file mode 100644 index 00000000..3e830445 --- /dev/null +++ b/Xamarin.Forms.Core/MenuItem.cs @@ -0,0 +1,117 @@ +using System; +using System.Windows.Input; + +namespace Xamarin.Forms +{ + public class MenuItem : BaseMenuItem + { + public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(MenuItem), null); + + public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(MenuItem), null, + propertyChanging: (bo, o, n) => ((MenuItem)bo).OnCommandChanging(), propertyChanged: (bo, o, n) => ((MenuItem)bo).OnCommandChanged()); + + public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create("CommandParameter", typeof(object), typeof(MenuItem), null, + propertyChanged: (bo, o, n) => ((MenuItem)bo).OnCommandParameterChanged()); + + public static readonly BindableProperty IsDestructiveProperty = BindableProperty.Create("IsDestructive", typeof(bool), typeof(MenuItem), false); + + public static readonly BindableProperty IconProperty = BindableProperty.Create("Icon", typeof(FileImageSource), typeof(MenuItem), default(FileImageSource)); + + internal static readonly BindableProperty IsEnabledProperty = BindableProperty.Create("IsEnabled", typeof(bool), typeof(ToolbarItem), true); + + public ICommand Command + { + get { return (ICommand)GetValue(CommandProperty); } + set { SetValue(CommandProperty, value); } + } + + public object CommandParameter + { + get { return GetValue(CommandParameterProperty); } + set { SetValue(CommandParameterProperty, value); } + } + + public FileImageSource Icon + { + get { return (FileImageSource)GetValue(IconProperty); } + set { SetValue(IconProperty, value); } + } + + public bool IsDestructive + { + get { return (bool)GetValue(IsDestructiveProperty); } + set { SetValue(IsDestructiveProperty, value); } + } + + public string Text + { + get { return (string)GetValue(TextProperty); } + set { SetValue(TextProperty, value); } + } + + internal bool IsEnabled + { + get { return (bool)GetValue(IsEnabledProperty); } + set { SetValue(IsEnabledProperty, value); } + } + + bool IsEnabledCore + { + set { SetValueCore(IsEnabledProperty, value); } + } + + public event EventHandler Clicked; + + protected virtual void OnClicked() + { + EventHandler handler = Clicked; + if (handler != null) + handler(this, EventArgs.Empty); + } + + internal void Activate() + { + if (Command != null) + { + if (IsEnabled) + Command.Execute(CommandParameter); + } + + OnClicked(); + } + + void OnCommandCanExecuteChanged(object sender, EventArgs eventArgs) + { + IsEnabledCore = Command.CanExecute(CommandParameter); + } + + void OnCommandChanged() + { + if (Command == null) + { + IsEnabledCore = true; + return; + } + + IsEnabledCore = Command.CanExecute(CommandParameter); + + Command.CanExecuteChanged += OnCommandCanExecuteChanged; + } + + void OnCommandChanging() + { + if (Command == null) + return; + + Command.CanExecuteChanged -= OnCommandCanExecuteChanged; + } + + void OnCommandParameterChanged() + { + if (Command == null) + return; + + IsEnabledCore = Command.CanExecute(CommandParameter); + } + } +}
\ No newline at end of file |