PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/PrismLibrary/Desktop/Prism/Commands/CommandBehaviorBase.cs

#
C# | 140 lines | 83 code | 12 blank | 45 comment | 13 complexity | df2d784f93146c43de0e377d09328392 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.Controls;
  20. using System.Windows.Input;
  21. namespace Microsoft.Practices.Prism.Commands
  22. {
  23. /// <summary>
  24. /// Base behavior to handle connecting a <see cref="Control"/> to a Command.
  25. /// </summary>
  26. /// <typeparam name="T">The target object must derive from Control</typeparam>
  27. /// <remarks>
  28. /// CommandBehaviorBase can be used to provide new behaviors for commands.
  29. /// </remarks>
  30. public class CommandBehaviorBase<T>
  31. #if SILVERLIGHT
  32. where T : Control
  33. #else
  34. where T : UIElement
  35. #endif
  36. {
  37. private ICommand command;
  38. private object commandParameter;
  39. private readonly WeakReference targetObject;
  40. private readonly EventHandler commandCanExecuteChangedHandler;
  41. /// <summary>
  42. /// Constructor specifying the target object.
  43. /// </summary>
  44. /// <param name="targetObject">The target object the behavior is attached to.</param>
  45. public CommandBehaviorBase(T targetObject)
  46. {
  47. this.targetObject = new WeakReference(targetObject);
  48. // In Silverlight, unlike WPF, this is strictly not necessary since the Command properties
  49. // in Silverlight do not expect their CanExecuteChanged handlers to be weakly held,
  50. // but holding on to them in this manner should do no harm.
  51. this.commandCanExecuteChangedHandler = new EventHandler(this.CommandCanExecuteChanged);
  52. }
  53. /// <summary>
  54. /// Corresponding command to be execute and monitored for <see cref="ICommand.CanExecuteChanged"/>
  55. /// </summary>
  56. public ICommand Command
  57. {
  58. get { return command; }
  59. set
  60. {
  61. if (this.command != null)
  62. {
  63. this.command.CanExecuteChanged -= this.commandCanExecuteChangedHandler;
  64. }
  65. this.command = value;
  66. if (this.command != null)
  67. {
  68. this.command.CanExecuteChanged += this.commandCanExecuteChangedHandler;
  69. UpdateEnabledState();
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// The parameter to supply the command during execution
  75. /// </summary>
  76. public object CommandParameter
  77. {
  78. get { return this.commandParameter; }
  79. set
  80. {
  81. if (this.commandParameter != value)
  82. {
  83. this.commandParameter = value;
  84. this.UpdateEnabledState();
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// Object to which this behavior is attached.
  90. /// </summary>
  91. protected T TargetObject
  92. {
  93. get
  94. {
  95. return targetObject.Target as T;
  96. }
  97. }
  98. /// <summary>
  99. /// Updates the target object's IsEnabled property based on the commands ability to execute.
  100. /// </summary>
  101. protected virtual void UpdateEnabledState()
  102. {
  103. if (TargetObject == null)
  104. {
  105. this.Command = null;
  106. this.CommandParameter = null;
  107. }
  108. else if (this.Command != null)
  109. {
  110. TargetObject.IsEnabled = this.Command.CanExecute(this.CommandParameter);
  111. }
  112. }
  113. private void CommandCanExecuteChanged(object sender, EventArgs e)
  114. {
  115. this.UpdateEnabledState();
  116. }
  117. /// <summary>
  118. /// Executes the command, if it's set, providing the <see cref="CommandParameter"/>
  119. /// </summary>
  120. protected virtual void ExecuteCommand()
  121. {
  122. if (this.Command != null)
  123. {
  124. this.Command.Execute(this.CommandParameter);
  125. }
  126. }
  127. }
  128. }