Fix TextBlock wrong caret position #14627
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does the pull request do?
Corrects caret behavior after inserting, losing window focus (detected during debugging) , or programmatically changing a TextBox.
What is the current behavior?
After selecting text from right to left and inserting the same number of characters in the same place or losing window focus, the caret position is displayed before the last (left) character selected. At the same time, after starting to enter new text, it is entered after the first(right) character.
The actual caret position and its display are not the same.
What is the updated/expected behavior with this PR?
The actual caret position and display now remain the same after pasting, losing window focus, or changing programmatically.
How was the solution implemented (if it's not obvious)?
Inserting text of the same length does not trigger OnPropertyChanged for CaretIndex.
After insertion, only SelectionEnd in TextBox.cs fires and no one updates CharacterHit _lastCharacterHit in TextPresenter.cs. Same reason for changing the text field programmatically.
TextPresenter.Render(DrawingContext) uses the old index from CharacterHit _lastCharacterHit for rendering.
Added MoveCaretToTextPosition call when changing SelectionEndProperty in TextBox.cs.
Checklist
Breaking changes
Obsoletions / Deprecations
Fixed issues
Fixes #13648
Fixes #13886
Fixes #14563
Future
In general, after losing the window's focus and returning it, the selected text should remain and not show the caret, as is implemented everywhere (I don't know MacOS).