Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Core / CSharp / System / Windows / Input / Command / CommandBinding.cs / 1 / CommandBinding.cs
//---------------------------------------------------------------------------- // // Copyright (C) Microsoft Corporation. All rights reserved. // //--------------------------------------------------------------------------- using System; using System.ComponentModel; using System.Windows; using System.Windows.Markup; using MS.Internal; using System.Security; using System.Security.Permissions; namespace System.Windows.Input { ////// CommandBinding - Command-EventHandlers map /// CommandBinding acts like a map for EventHandlers and Commands. /// PreviewExecute/Execute, PreviewCanExecute/CanExecute handlers /// can be added at CommandBinding which will exist at Element level /// in the form of a Collection and will be invoked when the system /// is routing the corresponding RoutedEvents. /// public class CommandBinding { #region Constructors ////// Default Constructor - required to allow creation from markup /// public CommandBinding() { } ////// Constructor /// /// Command associated with this binding. public CommandBinding(ICommand command) : this(command, null, null) { } ////// Constructor /// /// Command associated with this binding. /// Handler associated with executing the command. public CommandBinding(ICommand command, ExecutedRoutedEventHandler executed) : this(command, executed, null) { } ////// Constructor /// /// Command associated with this binding. /// Handler associated with executing the command. /// Handler associated with determining if the command can execute. public CommandBinding(ICommand command, ExecutedRoutedEventHandler executed, CanExecuteRoutedEventHandler canExecute) { if (command == null) { throw new ArgumentNullException("command"); } _command = command; if (executed != null) { Executed += executed; } if (canExecute != null) { CanExecute += canExecute; } } #endregion #region Public Properties ////// Command associated with this binding /// [Localizability(LocalizationCategory.NeverLocalize)] // cannot be localized public ICommand Command { get { return _command; } set { if (value == null) { throw new ArgumentNullException("value"); } _command = value; } } #endregion #region Public Events ////// Called before the command is executed. /// public event ExecutedRoutedEventHandler PreviewExecuted; ////// Called when the command is executed. /// public event ExecutedRoutedEventHandler Executed; ////// Called before determining if the command can be executed. /// public event CanExecuteRoutedEventHandler PreviewCanExecute; ////// Called to determine if the command can be executed. /// public event CanExecuteRoutedEventHandler CanExecute; #endregion #region Implementation ////// Calls the CanExecute or PreviewCanExecute event based on the event argument's RoutedEvent. /// /// The sender of the event. /// Event arguments. internal void OnCanExecute(object sender, CanExecuteRoutedEventArgs e) { if (!e.Handled) { if (e.RoutedEvent == CommandManager.CanExecuteEvent) { if (CanExecute != null) { CanExecute(sender, e); if (e.CanExecute) { e.Handled = true; } } else if (!e.CanExecute) { // If there is an Executed handler, then the command can be executed. if (Executed != null) { e.CanExecute = true; e.Handled = true; } } } else // e.RoutedEvent == CommandManager.PreviewCanExecuteEvent { if (PreviewCanExecute != null) { PreviewCanExecute(sender, e); if (e.CanExecute) { e.Handled = true; } } } } } private bool CheckCanExecute(object sender, ExecutedRoutedEventArgs e) { CanExecuteRoutedEventArgs canExecuteArgs = new CanExecuteRoutedEventArgs(e.Command, e.Parameter); canExecuteArgs.RoutedEvent = CommandManager.CanExecuteEvent; // Since we don't actually raise this event, we have to explicitly set the source. canExecuteArgs.Source = e.OriginalSource; canExecuteArgs.OverrideSource(e.Source); OnCanExecute(sender, canExecuteArgs); return canExecuteArgs.CanExecute; } ////// Calls Executed or PreviewExecuted based on the event argument's RoutedEvent. /// /// The sender of the event. /// Event arguments. internal void OnExecuted(object sender, ExecutedRoutedEventArgs e) { if (!e.Handled) { if (e.RoutedEvent == CommandManager.ExecutedEvent) { if (Executed != null) { if (CheckCanExecute(sender, e)) { Executed(sender, e); e.Handled = true; } } } else // e.RoutedEvent == CommandManager.PreviewExecutedEvent { if (PreviewExecuted != null) { if (CheckCanExecute(sender, e)) { PreviewExecuted(sender, e); e.Handled = true; } } } } } #endregion #region Data private ICommand _command; #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //---------------------------------------------------------------------------- // // Copyright (C) Microsoft Corporation. All rights reserved. // //--------------------------------------------------------------------------- using System; using System.ComponentModel; using System.Windows; using System.Windows.Markup; using MS.Internal; using System.Security; using System.Security.Permissions; namespace System.Windows.Input { ////// CommandBinding - Command-EventHandlers map /// CommandBinding acts like a map for EventHandlers and Commands. /// PreviewExecute/Execute, PreviewCanExecute/CanExecute handlers /// can be added at CommandBinding which will exist at Element level /// in the form of a Collection and will be invoked when the system /// is routing the corresponding RoutedEvents. /// public class CommandBinding { #region Constructors ////// Default Constructor - required to allow creation from markup /// public CommandBinding() { } ////// Constructor /// /// Command associated with this binding. public CommandBinding(ICommand command) : this(command, null, null) { } ////// Constructor /// /// Command associated with this binding. /// Handler associated with executing the command. public CommandBinding(ICommand command, ExecutedRoutedEventHandler executed) : this(command, executed, null) { } ////// Constructor /// /// Command associated with this binding. /// Handler associated with executing the command. /// Handler associated with determining if the command can execute. public CommandBinding(ICommand command, ExecutedRoutedEventHandler executed, CanExecuteRoutedEventHandler canExecute) { if (command == null) { throw new ArgumentNullException("command"); } _command = command; if (executed != null) { Executed += executed; } if (canExecute != null) { CanExecute += canExecute; } } #endregion #region Public Properties ////// Command associated with this binding /// [Localizability(LocalizationCategory.NeverLocalize)] // cannot be localized public ICommand Command { get { return _command; } set { if (value == null) { throw new ArgumentNullException("value"); } _command = value; } } #endregion #region Public Events ////// Called before the command is executed. /// public event ExecutedRoutedEventHandler PreviewExecuted; ////// Called when the command is executed. /// public event ExecutedRoutedEventHandler Executed; ////// Called before determining if the command can be executed. /// public event CanExecuteRoutedEventHandler PreviewCanExecute; ////// Called to determine if the command can be executed. /// public event CanExecuteRoutedEventHandler CanExecute; #endregion #region Implementation ////// Calls the CanExecute or PreviewCanExecute event based on the event argument's RoutedEvent. /// /// The sender of the event. /// Event arguments. internal void OnCanExecute(object sender, CanExecuteRoutedEventArgs e) { if (!e.Handled) { if (e.RoutedEvent == CommandManager.CanExecuteEvent) { if (CanExecute != null) { CanExecute(sender, e); if (e.CanExecute) { e.Handled = true; } } else if (!e.CanExecute) { // If there is an Executed handler, then the command can be executed. if (Executed != null) { e.CanExecute = true; e.Handled = true; } } } else // e.RoutedEvent == CommandManager.PreviewCanExecuteEvent { if (PreviewCanExecute != null) { PreviewCanExecute(sender, e); if (e.CanExecute) { e.Handled = true; } } } } } private bool CheckCanExecute(object sender, ExecutedRoutedEventArgs e) { CanExecuteRoutedEventArgs canExecuteArgs = new CanExecuteRoutedEventArgs(e.Command, e.Parameter); canExecuteArgs.RoutedEvent = CommandManager.CanExecuteEvent; // Since we don't actually raise this event, we have to explicitly set the source. canExecuteArgs.Source = e.OriginalSource; canExecuteArgs.OverrideSource(e.Source); OnCanExecute(sender, canExecuteArgs); return canExecuteArgs.CanExecute; } ////// Calls Executed or PreviewExecuted based on the event argument's RoutedEvent. /// /// The sender of the event. /// Event arguments. internal void OnExecuted(object sender, ExecutedRoutedEventArgs e) { if (!e.Handled) { if (e.RoutedEvent == CommandManager.ExecutedEvent) { if (Executed != null) { if (CheckCanExecute(sender, e)) { Executed(sender, e); e.Handled = true; } } } else // e.RoutedEvent == CommandManager.PreviewExecutedEvent { if (PreviewExecuted != null) { if (CheckCanExecute(sender, e)) { PreviewExecuted(sender, e); e.Handled = true; } } } } } #endregion #region Data private ICommand _command; #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ADMembershipProvider.cs
- XmlSchemaDatatype.cs
- COM2IManagedPerPropertyBrowsingHandler.cs
- ResourceSet.cs
- XNodeNavigator.cs
- _ConnectStream.cs
- SafeProcessHandle.cs
- Table.cs
- SpStreamWrapper.cs
- ProfilePropertySettings.cs
- SettingsBase.cs
- Span.cs
- DocumentApplicationJournalEntry.cs
- DocumentSequenceHighlightLayer.cs
- WindowsTooltip.cs
- BamlTreeNode.cs
- TdsParserStateObject.cs
- ConditionalAttribute.cs
- BasicHttpSecurityMode.cs
- validationstate.cs
- IdleTimeoutMonitor.cs
- UnicodeEncoding.cs
- MemberHolder.cs
- EntityDataSourceChangingEventArgs.cs
- CustomAttribute.cs
- WSSecureConversationFeb2005.cs
- ActivityExecutorSurrogate.cs
- Effect.cs
- LocalizationParserHooks.cs
- PackageRelationshipSelector.cs
- Triangle.cs
- StringFunctions.cs
- Operators.cs
- PolyBezierSegment.cs
- PropertyGridCommands.cs
- PrivacyNoticeBindingElement.cs
- Process.cs
- SoapIgnoreAttribute.cs
- AttachedPropertiesService.cs
- RouteCollection.cs
- SoapTypeAttribute.cs
- UriTemplateLiteralQueryValue.cs
- SizeLimitedCache.cs
- PeerCustomResolverSettings.cs
- InfoCardHelper.cs
- DoWorkEventArgs.cs
- VSWCFServiceContractGenerator.cs
- RegisteredDisposeScript.cs
- UnsafeNativeMethods.cs
- ListBoxChrome.cs
- SingleConverter.cs
- HwndSourceKeyboardInputSite.cs
- ScriptingAuthenticationServiceSection.cs
- ApplicationCommands.cs
- COM2TypeInfoProcessor.cs
- PartialCachingControl.cs
- ContractInferenceHelper.cs
- SecurityAlgorithmSuiteConverter.cs
- COM2ExtendedBrowsingHandler.cs
- SchemaTypeEmitter.cs
- SessionStateItemCollection.cs
- DesignerDataParameter.cs
- SiteMapProvider.cs
- UTF7Encoding.cs
- FilterQuery.cs
- figurelengthconverter.cs
- StyleXamlParser.cs
- EncoderNLS.cs
- SafeEventHandle.cs
- DocumentViewerAutomationPeer.cs
- DataGridViewCellConverter.cs
- OperandQuery.cs
- PackagePartCollection.cs
- ProtocolsConfiguration.cs
- TextPointerBase.cs
- ProfileGroupSettings.cs
- FontUnitConverter.cs
- DatePickerAutomationPeer.cs
- DelegatingConfigHost.cs
- ServicesExceptionNotHandledEventArgs.cs
- ListViewEditEventArgs.cs
- ConstraintEnumerator.cs
- BlockExpression.cs
- WhitespaceRule.cs
- CanonicalXml.cs
- RegistryConfigurationProvider.cs
- HuffmanTree.cs
- TextElementEnumerator.cs
- Decimal.cs
- Drawing.cs
- ToolStripDropDown.cs
- ObjectView.cs
- FrameworkRichTextComposition.cs
- ChineseLunisolarCalendar.cs
- DataViewSetting.cs
- StringAnimationUsingKeyFrames.cs
- RawStylusInputCustomData.cs
- ZipIOFileItemStream.cs
- FontWeights.cs
- Duration.cs