//private static void TextBox_GotFocus(object sender, RoutedEventArgs e) //{ // if (((TextBox)sender).SelectionLength == 0) // ((TextBox)sender).SelectAll(); //} public TextBoxReadOnlyCaretVisible() { //EventManager.RegisterClassHandler(typeof(TextBox), // UIElement.GotFocusEvent, // new RoutedEventHandler(TextBox_GotFocus)); AcceptsTab = false; AcceptsReturn = false; IsReadOnly = false; HorizontalAlignment = HorizontalAlignment.Stretch; VerticalAlignment = VerticalAlignment.Stretch; TextWrapping = TextWrapping.Wrap; // EQUIVALENT of DynamicResource in XAML //SetResourceReference(Control.BackgroundProperty, SystemColors.ControlLightLightBrushKey); SetResourceReference(Control.BorderBrushProperty, SystemColors.ControlDarkBrushKey); //Background = SystemColors.ControlLightLightBrush; //BorderBrush = SystemColors.ControlDarkDarkBrush; SnapsToDevicePixels = true; #if NET40 ////TODO: //TextBox.CaretBrush //TextBox.SelectionBrush #endif GotFocus += ((sender, e) => { if (!string.IsNullOrEmpty(SelectedText)) { SetAccessibleNameAndNotifyScreenReaderAutomationIfKeyboardFocused(SelectedText); } }); var tbSel = new TextBoxSelection(); SelectionChanged += ((sender, e) => { tbSel.start = SelectionStart; tbSel.length = SelectionLength; if (!string.IsNullOrEmpty(SelectedText)) { SetAccessibleNameAndNotifyScreenReaderAutomationIfKeyboardFocused(SelectedText); } }); TextChanged += ((sender, e) => { if (!string.IsNullOrEmpty(m_Text)) { int start = tbSel.start; int length = tbSel.length; Text = m_Text; Select(start, length); } }); }
protected override void UpdateImpl() { if (text != host.TextBoxText) { if (host.TextBoxText.Length >= MaxTextLength) { host.TextBoxText = host.TextBoxText.Substring(0, MaxTextLength); text = host.TextBoxText; } OnTextChanged(); _lastText = text; text = host.TextBoxText; } if (lastCaretIndex != host.TextBoxCaretIndex || lastSelection != host.TextBoxSelection || _lastText != text) { if (scrollIndex >= host.TextBoxCaretIndex) { do { scrollIndex--; } while (scrollIndex >= text.Length); if (scrollIndex < 0) { scrollIndex = 0; } stringObj.Text = text.Substring(scrollIndex); stringObj.Update(); } else { float tempWidth = 0; scrollIndex--; do { scrollIndex++; stringObj.Text = text.Substring(scrollIndex); stringObj.Update(); tempWidth = stringObj.GetWidthToIndex(host.TextBoxCaretIndex - scrollIndex); } while (tempWidth >= TextBoxWidth); } _lastText = text; lastSelection = host.TextBoxSelection; lastCaretIndex = host.TextBoxCaretIndex; } float p1 = host.TextBoxSelection.SelectionStart < scrollIndex?stringObj.GetWidthToIndex(0) : stringObj.GetWidthToIndex(host.TextBoxSelection.SelectionStart - scrollIndex), p2 = host.TextBoxSelection.SelectionEnd - scrollIndex >= stringObj.Text.Length ? stringObj.GetWidthToIndex(stringObj.Text.Length) : stringObj.GetWidthToIndex(host.TextBoxSelection.SelectionEnd - scrollIndex); var width = stringObj.GetWidthToIndex(host.TextBoxCaretIndex - scrollIndex); caret.Position = new Vector2(width, 0); caret.RectangleWidth = caretWidth; caret.RectangleHeight = TextBoxHeight + caretMargin; selection.Position = new Vector2(p1, 0); selection.RectangleWidth = p2 - p1; selection.RectangleHeight = TextBoxHeight + caretMargin; back.RectangleWidth = Math.Max(TextBoxWidth, stringObj.JustWidth); back.RectangleHeight = stringObj.CharacterHeight + caretMargin; border.Position = new Vector2(-thickness, -thickness); border.BorderThickness = thickness; border.RectangleWidth = Math.Max(TextBoxWidth, stringObj.JustWidth); border.RectangleHeight = stringObj.CharacterHeight + caretMargin + thickness; count++; if (count >= 60) { count = 0; } }