WPF TextBox - programmatically selects text while maintaining the "direction of selection"

When editing text, the user can use the button to switch keys and direction keys to change the selection - one position remains attached and the other moves. By clicking left, you can get the moving part to the left of the fixed part.

I am trying to dynamically modify the selection of users in a WPF TextBox (for discussion, say, I want to select characters in pairs, for example, by pressing shift + left, 2 characters will be selected, not just one). However, when using SelectionStart / SelectionIndex / CaretIndex / Select, the "end end of the selection engine" always ends at the far right end of the selection, making the offset + left combination useless.

Any way to keep the "direction of choice"?

+4
source share
4 answers

This is not exactly the answer you are looking for, but it will work. Call the command and do as WPF does.

EditingCommands.SelectLeftByCharacter.Execute(null, textBox1); 
+5
source

None of these works. SelectionLength cannot be negative, and CaretIndex is always identical to SelectionStart. This is a design error in TB, because you cannot achieve the correct state even in a protected area.

The command really works, but you still cannot determine the direction of the currently analyzed text field. There is only one solution, which is an ugly workaround: you can write your own observer code by overriding the mouse and keyboard event handlers.

+2
source

The only possibility I can think of is to set SelectionStart and then make SelectionLength negative.

0
source

Have you tried setting the CaretIndex property before or after setting SelectionStart / SelectionLength?

0
source

All Articles