/// <summary> /// Enforces selection rules based on current/maximum length and trying over to next control in a group. /// </summary> protected override void OnBeforeSelectionChanged(object sender, DynamicTextSelectionChangedEventArgs args) { // Never select the sign bool?sign = null; var text = Text; if (NumberBase == NumericTextBoxNumberBase.Decimal) { if (text.StartsWith("+", StringComparison.OrdinalIgnoreCase)) { sign = true; } else if (text.StartsWith("-", StringComparison.OrdinalIgnoreCase)) { sign = false; } } if (sign.HasValue && args.SelectionStart == 0) { args.SelectionStart = 1; } // Apply base class rules base.OnBeforeSelectionChanged(sender, args); }
/// <summary> /// Handles the selection changed event. /// </summary> private void OnSelectionChanged(object sender, RoutedEventArgs args) { // Apply rules var change = new DynamicTextSelectionChangedEventArgs(this, SelectionStart, SelectionLength); OnBeforeSelectionChanged(this, change); // Change focus when necessary var newFocus = change.Focus; if (newFocus != this) { newFocus.Focus(FocusState.Keyboard); return; } // Set new selection index and length SelectionStart = change.SelectionStart; SelectionLength = change.SelectionLength; // Update cursor LayoutCursor(); }
/// <summary> /// Enforces selection rules based on current/maximum length and trying over to next control in a group. /// </summary> protected virtual void OnBeforeSelectionChanged(object sender, DynamicTextSelectionChangedEventArgs args) { // Move to next control (if any) when at edge... var nextControl = GroupNext; var textLength = Text.Length; var selectedIndex = args.SelectionStart; var maxLength = MaxLength; if (maxLength > 0 && selectedIndex >= maxLength && nextControl != null) { // Set new control with cursor at start args.Focus = nextControl; args.SelectionStart = 0; args.SelectionLength = 0; return; } // Select one character (over-type) when within text but is at maximum length if (maxLength > 0 && textLength >= maxLength) { // Move back one character at end if (selectedIndex >= maxLength) { args.SelectionStart = selectedIndex - 1; } // Select one character args.SelectionLength = 1; } // Over-type when only remaining character is pad character if (textLength == 1 && PadChar.HasValue && Text == PadChar.Value.ToString()) { args.SelectionStart = 0; args.SelectionLength = 1; } }