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