using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Input; namespace Sample { /////////////////////////////////////////////////////////////////////////// /// /// Defines the different behaviors of a key. /// /////////////////////////////////////////////////////////////////////////// public enum KeyBehavior { /////////////////////////////////////////////////////////////////////////// /// The default behavior. /////////////////////////////////////////////////////////////////////////// None, /////////////////////////////////////////////////////////////////////////// /// Moves focus to the next control. /////////////////////////////////////////////////////////////////////////// NavigateNext } /////////////////////////////////////////////////////////////////////////// /// /// Provides different behaviors for a key press. /// /////////////////////////////////////////////////////////////////////////// public static class KeyBehaviors { /////////////////////////////////////////////////////////////////////////// /// /// Attached property that allows the Enter key to have other behaviors. /// /////////////////////////////////////////////////////////////////////////// public static readonly DependencyProperty EnterKeyBehaviorProperty = DependencyProperty.RegisterAttached("EnterKeyBehavior", typeof(KeyBehavior), typeof(KeyBehaviors), new PropertyMetadata(KeyBehavior.None, OnEnterKeyBehaviorChanged)); /////////////////////////////////////////////////////////////////////////// /// /// Sets the EnterKeyBehavior dependency property. /// /// The source. /// The value. /////////////////////////////////////////////////////////////////////////// public static void SetEnterKeyBehavior(DependencyObject source, KeyBehavior value) { source.SetValue(EnterKeyBehaviorProperty, value); } /////////////////////////////////////////////////////////////////////////// /// /// Gets the EnterKeyBehavior dependency property. /// /// The source. /////////////////////////////////////////////////////////////////////////// public static KeyBehavior GetEnterKeyBehavior(DependencyObject source) { return (KeyBehavior)source.GetValue(EnterKeyBehaviorProperty); } /////////////////////////////////////////////////////////////////////////// /// /// Called when the Behavior property changes on something. /// /// The source. /// The instance containing the event data. /////////////////////////////////////////////////////////////////////////// private static void OnEnterKeyBehaviorChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) { UIElement item = source as UIElement; if (item == null) { throw new InvalidOperationException("The Behavior property is only valid on UIElements."); } if ((KeyBehavior)e.NewValue == KeyBehavior.NavigateNext) { // // Hook into the preview key down event. // item.PreviewKeyDown += new KeyEventHandler(OnPreviewKeyDown); } else if ((KeyBehavior)e.NewValue == KeyBehavior.None) { item.PreviewKeyDown -= new KeyEventHandler(OnPreviewKeyDown); } } /////////////////////////////////////////////////////////////////////////// /// /// Called when a key is pressed on a control that we're attached to. /// /// The sender. /// The instance containing the event data. ////////////////////////////////////////////////////////////////////////////// private static void OnPreviewKeyDown(object sender, KeyEventArgs e) { UIElement item = e.Source as UIElement; if (item != null) { switch(e.Key) { case Key.Enter: bool result = item.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); break; } } } } }