/// <summary> /// Provides handling for the /// <see cref="E:System.Windows.UIElement.KeyDown" /> event. /// </summary> /// <param name="e">Key event args.</param> protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if (e.Handled || !IsEnabled) { return; } // Some keys (e.g. Left/Right) need to be translated in RightToLeft mode Key invariantKey = InteractionHelper.GetLogicalKey(FlowDirection, e.Key); bool isExpanded = IsExpanded; switch (ExpandDirection) { case ExpandDirection.Down: if ((isExpanded && invariantKey == Key.Up) || (!isExpanded && invariantKey == Key.Down)) { IsExpanded = !isExpanded; } break; case ExpandDirection.Up: if ((isExpanded && invariantKey == Key.Down) || (!isExpanded && invariantKey == Key.Up)) { IsExpanded = !isExpanded; } break; case ExpandDirection.Left: if ((isExpanded && invariantKey == Key.Right) || (!isExpanded && invariantKey == Key.Left)) { IsExpanded = !isExpanded; } break; case ExpandDirection.Right: if ((isExpanded && invariantKey == Key.Left) || (!isExpanded && invariantKey == Key.Right)) { IsExpanded = !isExpanded; } break; } }
/// <summary> /// This is the method that responds to the KeyDown event. /// </summary> /// <param name="e"> /// A <see cref="T:System.Windows.Input.KeyEventArgs" /> that contains /// the event data. /// </param> protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if (e.Handled) { return; } // Some keys (e.g. Left/Right) need to be translated in RightToLeft mode Key invariantKey = InteractionHelper.GetLogicalKey(FlowDirection, e.Key); TabItem nextTabItem = null; int direction = 0; int startIndex = TabControlParent.Items.IndexOf(this); switch (invariantKey) { case Key.Right: case Key.Down: direction = 1; break; case Key.Left: case Key.Up: direction = -1; break; default: return; } nextTabItem = TabControlParent.FindNextTabItem(startIndex, direction); if (nextTabItem != null && nextTabItem != TabControlParent.SelectedItem) { e.Handled = true; TabControlParent.SelectedItem = nextTabItem; nextTabItem.Focus(); } }
/// <summary> /// Handle keys related to scrolling. /// </summary> /// <param name="key">The key to handle.</param> /// <returns>A value indicating whether the key was handled.</returns> private bool HandleScrollKeys(Key key) { ScrollViewer scrollHost = ItemsControlHelper.ScrollHost; if (scrollHost != null) { // Some keys (e.g. Left/Right) need to be translated in RightToLeft mode Key invariantKey = InteractionHelper.GetLogicalKey(FlowDirection, key); switch (invariantKey) { case Key.PageUp: // Move horizontally if we've run out of room vertically if (!NumericExtensions.IsGreaterThan(scrollHost.ExtentHeight, scrollHost.ViewportHeight)) { scrollHost.PageLeft(); } else { scrollHost.PageUp(); } return(true); case Key.PageDown: // Move horizontally if we've run out of room vertically if (!NumericExtensions.IsGreaterThan(scrollHost.ExtentHeight, scrollHost.ViewportHeight)) { scrollHost.PageRight(); } else { scrollHost.PageDown(); } return(true); case Key.Home: scrollHost.ScrollToTop(); return(true); case Key.End: scrollHost.ScrollToBottom(); return(true); case Key.Left: scrollHost.LineLeft(); return(true); case Key.Right: scrollHost.LineRight(); return(true); case Key.Up: scrollHost.LineUp(); return(true); case Key.Down: scrollHost.LineDown(); return(true); } } return(false); }
protected override void OnKeyDown(KeyEventArgs e) { if (!Interaction.AllowKeyDown(e)) { return; } base.OnKeyDown(e); if (e.Handled) { return; } // Some keys (e.g. Left/Right) need to be translated in RightToLeft mode Key invariantKey = InteractionHelper.GetLogicalKey(FlowDirection, e.Key); switch (invariantKey) { case Key.Left: { #if SILVERLIGHT RatingItem ratingItem = FocusManager.GetFocusedElement() as RatingItem; #else RatingItem ratingItem = FocusManager.GetFocusedElement(Application.Current.MainWindow) as RatingItem; #endif if (ratingItem != null) { ratingItem = GetRatingItemAtOffsetFrom(ratingItem, -1); } else { ratingItem = GetRatingItems().FirstOrDefault(); } if (ratingItem != null) { if (ratingItem.Focus()) { e.Handled = true; } } } break; case Key.Right: { #if SILVERLIGHT RatingItem ratingItem = FocusManager.GetFocusedElement() as RatingItem; #else RatingItem ratingItem = FocusManager.GetFocusedElement(Application.Current.MainWindow) as RatingItem; #endif if (ratingItem != null) { ratingItem = GetRatingItemAtOffsetFrom(ratingItem, 1); } else { ratingItem = GetRatingItems().FirstOrDefault(); } if (ratingItem != null) { if (ratingItem.Focus()) { e.Handled = true; } } } break; case Key.Add: { if (!this.IsReadOnly) { RatingItem ratingItem = GetSelectedRatingItem(); if (ratingItem != null) { ratingItem = GetRatingItemAtOffsetFrom(ratingItem, 1); } else { ratingItem = GetRatingItems().FirstOrDefault(); } if (ratingItem != null) { ratingItem.SelectValue(); e.Handled = true; } } } break; case Key.Subtract: { if (!this.IsReadOnly) { RatingItem ratingItem = GetSelectedRatingItem(); if (ratingItem != null) { ratingItem = GetRatingItemAtOffsetFrom(ratingItem, -1); } if (ratingItem != null) { ratingItem.SelectValue(); e.Handled = true; } } } break; } }