PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/PrismLibrary/Phone/Prism.Interactivity/ApplicationBarButtonCommand.cs

#
C# | 159 lines | 89 code | 18 blank | 52 comment | 6 complexity | 481725da7888a2ded138199b6ef68fe5 MD5 | raw file
  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System;
  18. using System.Windows;
  19. using System.Windows.Data;
  20. using System.Windows.Input;
  21. using System.Windows.Interactivity;
  22. using Microsoft.Phone.Controls;
  23. using Microsoft.Phone.Shell;
  24. namespace Microsoft.Practices.Prism.Interactivity
  25. {
  26. /// <summary>
  27. /// Associates a command to an <see cref="ApplicationBarIconButton"/>.
  28. /// </summary>
  29. [CLSCompliant(false)]
  30. public class ApplicationBarButtonCommand : Behavior<PhoneApplicationPage>
  31. {
  32. ///<summary>
  33. /// The parameter to use when calling methods on the <see cref="ICommand"/> interface.
  34. ///</summary>
  35. public static readonly DependencyProperty CommandParameterProperty =
  36. DependencyProperty.Register("CommandParameter",
  37. typeof(string),
  38. typeof(ApplicationBarButtonCommand),
  39. new PropertyMetadata(HandleCommandChanged));
  40. /// <summary>
  41. /// The binding for <see cref="ICommand"/> to invoke based on the ApplicationBarIconButton's events.
  42. /// </summary>
  43. public static readonly DependencyProperty CommandBindingProperty =
  44. DependencyProperty.Register("CommandBinding",
  45. typeof(ICommand),
  46. typeof(ApplicationBarButtonCommand),
  47. new PropertyMetadata(HandleCommandChanged));
  48. private ClickCommandBinding binding;
  49. /// <summary>
  50. /// The text indicating which <see cref="ApplicationBarIconButton"/> to bind with.
  51. /// </summary>
  52. public string ButtonText { get; set; }
  53. /// <summary>
  54. /// The <see cref="ICommand"/> associated with the instance of ApplicationBarIconButton.
  55. /// </summary>
  56. public ICommand CommandBinding
  57. {
  58. get { return (ICommand)GetValue(CommandBindingProperty); }
  59. set { SetValue(CommandBindingProperty, value); }
  60. }
  61. /// <summary>
  62. /// the string based parameter to be passed to the <see cref="ICommand"/>.
  63. /// </summary>
  64. public string CommandParameter
  65. {
  66. get { return (string)GetValue(CommandParameterProperty); }
  67. set { SetValue(CommandParameterProperty, value); }
  68. }
  69. /// <summary>
  70. /// Called after the behavior is attached to an AssociatedObject.
  71. /// </summary>
  72. /// <remarks>
  73. /// Override this to hook up functionality to the AssociatedObject.
  74. /// </remarks>
  75. protected override void OnAttached()
  76. {
  77. base.OnAttached();
  78. this.CreateBinding();
  79. }
  80. /// <summary>
  81. /// Invoked when the <see cref="CommandBinding"/> changes.
  82. /// </summary>
  83. protected void OnCommandChanged()
  84. {
  85. this.CreateBinding();
  86. }
  87. private static void HandleCommandChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  88. {
  89. ((ApplicationBarButtonCommand)sender).OnCommandChanged();
  90. }
  91. private void CreateBinding()
  92. {
  93. if (this.CommandBinding == null || this.AssociatedObject == null) return;
  94. if (this.binding != null)
  95. {
  96. this.binding.Detach();
  97. }
  98. this.binding = new ClickCommandBinding(
  99. this.AssociatedObject.ApplicationBar.FindButton(this.ButtonText),
  100. (ICommand)this.CommandBinding,
  101. () => this.CommandParameter);
  102. }
  103. /// <summary>
  104. /// Binds an <see cref="ApplicationBarIconButton"/> to a <see cref="ICommand"/>.
  105. /// </summary>
  106. private class ClickCommandBinding
  107. {
  108. private readonly ICommand command;
  109. private readonly ApplicationBarIconButton iconButton;
  110. private readonly Func<object> parameterGetter;
  111. /// <summary>
  112. ///
  113. /// </summary>
  114. /// <param name="iconButton"></param>
  115. /// <param name="command"></param>
  116. /// <param name="parameterGetter"></param>
  117. public ClickCommandBinding(ApplicationBarIconButton iconButton, ICommand command, Func<object> parameterGetter)
  118. {
  119. this.command = command;
  120. this.iconButton = iconButton;
  121. this.parameterGetter = parameterGetter;
  122. this.iconButton.IsEnabled = this.command.CanExecute(parameterGetter());
  123. this.command.CanExecuteChanged += this.CommandCanExecuteChanged;
  124. this.iconButton.Click += this.IconButtonClicked;
  125. }
  126. public void Detach()
  127. {
  128. this.iconButton.Click -= this.IconButtonClicked;
  129. this.command.CanExecuteChanged -= this.CommandCanExecuteChanged;
  130. }
  131. private void IconButtonClicked(object s, EventArgs e)
  132. {
  133. this.command.Execute(this.parameterGetter());
  134. }
  135. private void CommandCanExecuteChanged(object s, EventArgs ea)
  136. {
  137. this.iconButton.IsEnabled = this.command.CanExecute(this.parameterGetter());
  138. }
  139. }
  140. }
  141. }