/// <summary> /// Moves the caret in the specified direction. /// </summary> private Boolean MoveCaretInDirection(CaretNavigationDirection direction, ModifierKeys modifiers = ModifierKeys.None) { var owner = TemplatedParent as Control; var isReadOnly = (owner != null && owner.GetValue<Boolean>(TextBox.IsReadOnlyProperty)); var isReadOnlyCaretVisible = (owner != null && owner.GetValue<Boolean>(TextBox.IsReadOnlyCaretVisibleProperty)); if (isReadOnly && !isReadOnlyCaretVisible && (modifiers & ModifierKeys.Shift) != ModifierKeys.Shift) return false; switch (direction) { case CaretNavigationDirection.Left: MoveCaretLeft(); break; case CaretNavigationDirection.Right: MoveCaretRight(); break; case CaretNavigationDirection.Up: MoveCaretUp(); break; case CaretNavigationDirection.Down: MoveCaretDown(); break; case CaretNavigationDirection.Home: MoveCaretToHome((modifiers & ModifierKeys.Control) == ModifierKeys.Control); break; case CaretNavigationDirection.End: MoveCaretToEnd((modifiers & ModifierKeys.Control) == ModifierKeys.Control); break; case CaretNavigationDirection.PageUp: MoveCaretPageUp(); break; case CaretNavigationDirection.PageDown: MoveCaretPageDown(); break; } return true; }
/// <summary> /// Handles movement events which affect the selection. /// </summary> /// <param name="movementAllowed">A value indicating whether the user's desired movement is allowed.</param> /// <param name="movementDirection">A <see cref="CaretNavigationDirection"/> value specifying which direction the selection is moving.</param> /// <returns><see langword="true"/> if the movement was handled; otherwise, <see langword="false"/>.</returns> private Boolean HandleSelectionMovement(Boolean movementAllowed, CaretNavigationDirection movementDirection) { var selecting = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift; if (selecting) { if (selectionPosition == null) selectionPosition = caretPosition; return !movementAllowed; } else { if (selectionPosition == null) return !movementAllowed; var newCaretPosition = (movementDirection == CaretNavigationDirection.Left) ? SelectionStart : (movementDirection == CaretNavigationDirection.Right) ? SelectionStart + SelectionLength : (Int32?)null; selectionPosition = null; if (newCaretPosition.HasValue) { caretBlinkTimer = 0; caretPosition = newCaretPosition.Value; UpdateCaret(); UpdateSelection(); return true; } return !movementAllowed; } }