/// <summary> /// Called when this element or any below gets focus. /// </summary> private static void OnGotFocus(object sender, RoutedEventArgs e) { // When NumericUpDown gets logical focus, select the text inside us. // If we're an editable NumericUpDown, forward focus to the TextBox element if (!e.Handled) { NumericUpDown numericUpDown = (NumericUpDown)sender; if ((numericUpDown.InterceptManualEnter || numericUpDown.IsReadOnly) && numericUpDown.Focusable && e.OriginalSource == numericUpDown) { // MoveFocus takes a TraversalRequest as its argument. var request = new TraversalRequest((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift ? FocusNavigationDirection.Previous : FocusNavigationDirection.Next); // Gets the element with keyboard focus. var elementWithFocus = Keyboard.FocusedElement as UIElement; // Change keyboard focus. if (elementWithFocus != null) { elementWithFocus.MoveFocus(request); } else { numericUpDown.Focus(); } e.Handled = true; } } }
/// <summary> /// Called when a cell has just switched to edit mode. /// </summary> /// <param name="editingElement">A reference to element returned by GenerateEditingElement.</param> /// <param name="editingEventArgs">The event args of the input event that caused the cell to go into edit mode. May be null.</param> /// <returns>The unedited value of the cell.</returns> protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs) { NumericUpDown numericUpDown = editingElement as NumericUpDown; if (numericUpDown != null) { numericUpDown.Focus(); numericUpDown.SelectAll(); return(numericUpDown.Value); } return(null); }