Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / 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
- BamlStream.cs
- EventPropertyMap.cs
- SmiContextFactory.cs
- FontStyleConverter.cs
- BitmapMetadataEnumerator.cs
- SqlCharStream.cs
- XPathNavigatorKeyComparer.cs
- WindowsAuthenticationEventArgs.cs
- _SSPIWrapper.cs
- SQLCharsStorage.cs
- ProfileEventArgs.cs
- ColorKeyFrameCollection.cs
- TypeReference.cs
- DbProviderFactories.cs
- ToolboxItemAttribute.cs
- HttpModuleCollection.cs
- TypeConverters.cs
- SimpleType.cs
- HelpInfo.cs
- RequestTimeoutManager.cs
- PartialTrustValidationBehavior.cs
- InvariantComparer.cs
- OutputWindow.cs
- OperatorExpressions.cs
- IgnoreFileBuildProvider.cs
- BindingElementExtensionElement.cs
- CookieProtection.cs
- webeventbuffer.cs
- DesignerLoader.cs
- OleDbConnectionPoolGroupProviderInfo.cs
- SafeNativeMethods.cs
- TreeView.cs
- TransactionProtocolConverter.cs
- AbandonedMutexException.cs
- EntityDataSourceConfigureObjectContext.cs
- PrimitiveCodeDomSerializer.cs
- ModifierKeysConverter.cs
- RootProjectionNode.cs
- TreeBuilderXamlTranslator.cs
- ColorAnimationUsingKeyFrames.cs
- PointConverter.cs
- SqlDataSourceSelectingEventArgs.cs
- SystemTcpStatistics.cs
- EventTrigger.cs
- FileDataSourceCache.cs
- DataGridHelper.cs
- SamlSecurityToken.cs
- GradientBrush.cs
- NegotiationTokenProvider.cs
- SiteIdentityPermission.cs
- EDesignUtil.cs
- InvalidCardException.cs
- PolicyLevel.cs
- Events.cs
- SchemaTypeEmitter.cs
- SpAudioStreamWrapper.cs
- OutputCacheProfile.cs
- ConfigurationPropertyAttribute.cs
- CaseInsensitiveHashCodeProvider.cs
- UiaCoreTypesApi.cs
- SafeRightsManagementSessionHandle.cs
- SpeakCompletedEventArgs.cs
- EmptyEnumerable.cs
- LineServicesCallbacks.cs
- SchemaImporterExtensionElementCollection.cs
- ConfigViewGenerator.cs
- PlanCompiler.cs
- EditorReuseAttribute.cs
- ZipIOExtraFieldElement.cs
- Vector3D.cs
- XmlSchemaComplexType.cs
- ImageFormatConverter.cs
- KeyboardDevice.cs
- DynamicRenderer.cs
- SqlDataSourceCommandEventArgs.cs
- SectionVisual.cs
- UpdatePanelTriggerCollection.cs
- DeclaredTypeValidatorAttribute.cs
- ProcessStartInfo.cs
- ObjectListDesigner.cs
- MsmqTransportReceiveParameters.cs
- ApplicationServiceHelper.cs
- ListViewGroupCollectionEditor.cs
- SoapInteropTypes.cs
- RecognizedWordUnit.cs
- EntityContainerEmitter.cs
- BamlTreeMap.cs
- TextStore.cs
- AuthenticationException.cs
- Knowncolors.cs
- DesignBindingEditor.cs
- __ComObject.cs
- MouseActionValueSerializer.cs
- OpenTypeLayoutCache.cs
- RegistryKey.cs
- ReturnEventArgs.cs
- FrameworkContentElement.cs
- WindowsFormsSynchronizationContext.cs
- DefaultPrintController.cs
- ErrorRuntimeConfig.cs