Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Core / CSharp / System / Windows / Input / Command / KeyBinding.cs / 1305600 / KeyBinding.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description: The KeyBinding class is used by the developer to create Keyboard Input Bindings
//
// See spec at : http://avalon/coreui/Specs/Commanding(new).mht
//
//* KeyBinding class serves the purpose of Input Bindings for Keyboard Device.
//
// History:
// 06/01/2003 : chandras - Created
// 05/01/2004 : chandra - changed to accommodate new design
// ( http://avalon/coreui/Specs/Commanding(new).mht )
//---------------------------------------------------------------------------
using System;
using System.Windows.Input;
using System.Windows;
using System.ComponentModel;
using System.Windows.Markup;
using SR=MS.Internal.PresentationCore.SR;
using SRID=MS.Internal.PresentationCore.SRID;
namespace System.Windows.Input
{
///
/// KeyBinding - Implements InputBinding (generic InputGesture-Command map)
/// KeyBinding acts like a map for KeyGesture and Commands.
/// Most of the logic is in InputBinding and KeyGesture, this only
/// facilitates user to add Key/Modifiers directly without going in
/// KeyGesture path. Also it provides the KeyGestureTypeConverter
/// on the Gesture property to have KeyGesture, like Ctrl+X, Alt+V
/// defined in Markup as Gesture="Ctrl+X" working
///
public class KeyBinding : InputBinding
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
#region Constructor
///
/// Constructor
///
public KeyBinding() : base()
{
}
///
/// Constructor
///
/// Command associated
/// KeyGesture associated
public KeyBinding(ICommand command, KeyGesture gesture) : base(command, gesture)
{
SynchronizePropertiesFromGesture(gesture);
}
///
/// Constructor
///
///
/// modifiers
/// key
public KeyBinding(ICommand command, Key key, ModifierKeys modifiers) :
this(command, new KeyGesture(key, modifiers))
{
}
#endregion Constructor
//------------------------------------------------------
//
// Public Methods
//
//-----------------------------------------------------
#region Public Methods
///
/// KeyGesture Override, to ensure type-safety and provide a
/// TypeConverter for KeyGesture
///
[TypeConverter(typeof(KeyGestureConverter))]
[ValueSerializer(typeof(KeyGestureValueSerializer))]
public override InputGesture Gesture
{
get
{
return base.Gesture as KeyGesture;
}
set
{
KeyGesture keyGesture = value as KeyGesture;
if (keyGesture != null)
{
base.Gesture = value;
SynchronizePropertiesFromGesture(keyGesture);
}
else
{
throw new ArgumentException(SR.Get(SRID.InputBinding_ExpectedInputGesture, typeof(KeyGesture)));
}
}
}
///
/// Dependency Property for Modifiers
///
public static readonly DependencyProperty ModifiersProperty =
DependencyProperty.Register("Modifiers", typeof(ModifierKeys), typeof(KeyBinding), new UIPropertyMetadata(ModifierKeys.None, new PropertyChangedCallback(OnModifiersPropertyChanged)));
///
/// Modifiers
///
public ModifierKeys Modifiers
{
get
{
return (ModifierKeys)GetValue(ModifiersProperty);
}
set
{
SetValue(ModifiersProperty, value);
}
}
private static void OnModifiersPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
KeyBinding keyBinding = (KeyBinding)d;
keyBinding.SynchronizeGestureFromProperties(keyBinding.Key, (ModifierKeys)(e.NewValue));
}
///
/// Dependency Property for Key
///
public static readonly DependencyProperty KeyProperty =
DependencyProperty.Register("Key", typeof(Key), typeof(KeyBinding), new UIPropertyMetadata(Key.None, new PropertyChangedCallback(OnKeyPropertyChanged)));
///
/// Key
///
public Key Key
{
get
{
return (Key)GetValue(KeyProperty);
}
set
{
SetValue(KeyProperty, value);
}
}
private static void OnKeyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
KeyBinding keyBinding = (KeyBinding)d;
keyBinding.SynchronizeGestureFromProperties((Key)(e.NewValue), keyBinding.Modifiers);
}
#endregion Public Methods
#region Freezable
protected override Freezable CreateInstanceCore()
{
return new KeyBinding();
}
#endregion
#region Private Methods
///
/// Synchronized Properties from Gesture
///
private void SynchronizePropertiesFromGesture(KeyGesture keyGesture)
{
if (!_settingGesture)
{
_settingGesture = true;
try
{
Key = keyGesture.Key;
Modifiers = keyGesture.Modifiers;
}
finally
{
_settingGesture = false;
}
}
}
///
/// Synchronized Gesture from properties
///
private void SynchronizeGestureFromProperties(Key key, ModifierKeys modifiers)
{
if (!_settingGesture)
{
_settingGesture = true;
try
{
Gesture = new KeyGesture(key, modifiers, /*validateGesture = */ false);
}
finally
{
_settingGesture = false;
}
}
}
#endregion
//------------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
#region Data
private bool _settingGesture = false;
#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.
//
//
//
// Description: The KeyBinding class is used by the developer to create Keyboard Input Bindings
//
// See spec at : http://avalon/coreui/Specs/Commanding(new).mht
//
//* KeyBinding class serves the purpose of Input Bindings for Keyboard Device.
//
// History:
// 06/01/2003 : chandras - Created
// 05/01/2004 : chandra - changed to accommodate new design
// ( http://avalon/coreui/Specs/Commanding(new).mht )
//---------------------------------------------------------------------------
using System;
using System.Windows.Input;
using System.Windows;
using System.ComponentModel;
using System.Windows.Markup;
using SR=MS.Internal.PresentationCore.SR;
using SRID=MS.Internal.PresentationCore.SRID;
namespace System.Windows.Input
{
///
/// KeyBinding - Implements InputBinding (generic InputGesture-Command map)
/// KeyBinding acts like a map for KeyGesture and Commands.
/// Most of the logic is in InputBinding and KeyGesture, this only
/// facilitates user to add Key/Modifiers directly without going in
/// KeyGesture path. Also it provides the KeyGestureTypeConverter
/// on the Gesture property to have KeyGesture, like Ctrl+X, Alt+V
/// defined in Markup as Gesture="Ctrl+X" working
///
public class KeyBinding : InputBinding
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
#region Constructor
///
/// Constructor
///
public KeyBinding() : base()
{
}
///
/// Constructor
///
/// Command associated
/// KeyGesture associated
public KeyBinding(ICommand command, KeyGesture gesture) : base(command, gesture)
{
SynchronizePropertiesFromGesture(gesture);
}
///
/// Constructor
///
///
/// modifiers
/// key
public KeyBinding(ICommand command, Key key, ModifierKeys modifiers) :
this(command, new KeyGesture(key, modifiers))
{
}
#endregion Constructor
//------------------------------------------------------
//
// Public Methods
//
//-----------------------------------------------------
#region Public Methods
///
/// KeyGesture Override, to ensure type-safety and provide a
/// TypeConverter for KeyGesture
///
[TypeConverter(typeof(KeyGestureConverter))]
[ValueSerializer(typeof(KeyGestureValueSerializer))]
public override InputGesture Gesture
{
get
{
return base.Gesture as KeyGesture;
}
set
{
KeyGesture keyGesture = value as KeyGesture;
if (keyGesture != null)
{
base.Gesture = value;
SynchronizePropertiesFromGesture(keyGesture);
}
else
{
throw new ArgumentException(SR.Get(SRID.InputBinding_ExpectedInputGesture, typeof(KeyGesture)));
}
}
}
///
/// Dependency Property for Modifiers
///
public static readonly DependencyProperty ModifiersProperty =
DependencyProperty.Register("Modifiers", typeof(ModifierKeys), typeof(KeyBinding), new UIPropertyMetadata(ModifierKeys.None, new PropertyChangedCallback(OnModifiersPropertyChanged)));
///
/// Modifiers
///
public ModifierKeys Modifiers
{
get
{
return (ModifierKeys)GetValue(ModifiersProperty);
}
set
{
SetValue(ModifiersProperty, value);
}
}
private static void OnModifiersPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
KeyBinding keyBinding = (KeyBinding)d;
keyBinding.SynchronizeGestureFromProperties(keyBinding.Key, (ModifierKeys)(e.NewValue));
}
///
/// Dependency Property for Key
///
public static readonly DependencyProperty KeyProperty =
DependencyProperty.Register("Key", typeof(Key), typeof(KeyBinding), new UIPropertyMetadata(Key.None, new PropertyChangedCallback(OnKeyPropertyChanged)));
///
/// Key
///
public Key Key
{
get
{
return (Key)GetValue(KeyProperty);
}
set
{
SetValue(KeyProperty, value);
}
}
private static void OnKeyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
KeyBinding keyBinding = (KeyBinding)d;
keyBinding.SynchronizeGestureFromProperties((Key)(e.NewValue), keyBinding.Modifiers);
}
#endregion Public Methods
#region Freezable
protected override Freezable CreateInstanceCore()
{
return new KeyBinding();
}
#endregion
#region Private Methods
///
/// Synchronized Properties from Gesture
///
private void SynchronizePropertiesFromGesture(KeyGesture keyGesture)
{
if (!_settingGesture)
{
_settingGesture = true;
try
{
Key = keyGesture.Key;
Modifiers = keyGesture.Modifiers;
}
finally
{
_settingGesture = false;
}
}
}
///
/// Synchronized Gesture from properties
///
private void SynchronizeGestureFromProperties(Key key, ModifierKeys modifiers)
{
if (!_settingGesture)
{
_settingGesture = true;
try
{
Gesture = new KeyGesture(key, modifiers, /*validateGesture = */ false);
}
finally
{
_settingGesture = false;
}
}
}
#endregion
//------------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
#region Data
private bool _settingGesture = false;
#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
- PublisherIdentityPermission.cs
- RectAnimation.cs
- PeerContact.cs
- XmlSchemaGroup.cs
- DataSourceBooleanViewSchemaConverter.cs
- PageClientProxyGenerator.cs
- XmlSerializerFactory.cs
- DataColumnChangeEvent.cs
- TemplateAction.cs
- PackWebRequestFactory.cs
- EntitySetRetriever.cs
- SmiContext.cs
- StaticContext.cs
- WriteTimeStream.cs
- CustomValidator.cs
- SqlDataSourceStatusEventArgs.cs
- DataContractSerializerFaultFormatter.cs
- XmlSchemaSubstitutionGroup.cs
- CachingHintValidation.cs
- ProcessHostConfigUtils.cs
- DebugHandleTracker.cs
- MasterPage.cs
- InfoCardTrace.cs
- EmptyEnumerable.cs
- AppDomain.cs
- TextElementEditingBehaviorAttribute.cs
- ObjectDataSourceFilteringEventArgs.cs
- TextEditorLists.cs
- SmtpDateTime.cs
- ToolStripInSituService.cs
- ScrollViewerAutomationPeer.cs
- ConfigurationProperty.cs
- CuspData.cs
- infer.cs
- DecoderReplacementFallback.cs
- RegexStringValidator.cs
- HttpChannelFactory.cs
- SubpageParagraph.cs
- EnumBuilder.cs
- UnsafeNativeMethods.cs
- WhitespaceSignificantCollectionAttribute.cs
- DelimitedListTraceListener.cs
- BroadcastEventHelper.cs
- DataList.cs
- ApplicationFileParser.cs
- PropertyManager.cs
- OneToOneMappingSerializer.cs
- LinearQuaternionKeyFrame.cs
- HttpContextServiceHost.cs
- IApplicationTrustManager.cs
- ReflectEventDescriptor.cs
- GridViewCancelEditEventArgs.cs
- ReaderWriterLockWrapper.cs
- PathSegment.cs
- Clipboard.cs
- XmlSortKey.cs
- DataGridViewCellValidatingEventArgs.cs
- PrePrepareMethodAttribute.cs
- LinqDataSourceEditData.cs
- WorkflowNamespace.cs
- CompiledAction.cs
- _TimerThread.cs
- DataGridViewToolTip.cs
- EventRouteFactory.cs
- QilXmlWriter.cs
- RtfToXamlLexer.cs
- ImageListImage.cs
- ReflectionHelper.cs
- ToolBar.cs
- WS2007FederationHttpBinding.cs
- PrefixQName.cs
- ColorAnimation.cs
- InfoCardRSAPKCS1KeyExchangeFormatter.cs
- LineInfo.cs
- ResetableIterator.cs
- ExplicitDiscriminatorMap.cs
- DefaultValueAttribute.cs
- SspiWrapper.cs
- LabelEditEvent.cs
- PropertyMapper.cs
- WebEventTraceProvider.cs
- UnsafeNativeMethodsTablet.cs
- BooleanExpr.cs
- ExceptionHandlers.cs
- OleDbConnectionPoolGroupProviderInfo.cs
- NameValuePair.cs
- ProtocolsConfigurationEntry.cs
- ErrorFormatter.cs
- MergeFilterQuery.cs
- FormattedText.cs
- XmlStringTable.cs
- CodeGotoStatement.cs
- GroupQuery.cs
- ScrollItemProviderWrapper.cs
- DesignerTransactionCloseEvent.cs
- MouseActionValueSerializer.cs
- XmlSchemaType.cs
- WebServiceClientProxyGenerator.cs
- OdbcTransaction.cs
- AppSettingsExpressionBuilder.cs