简体   繁体   中英

Programmatically changing WPF TextBox's Text with regard of cursor position and current selection

I am implementing some sort of NumericKeypad Control for my WPF application which clients can use to conviniently enter text with a Touchscreen.

The Control itself runs fine, I even managed to run the Control (which is a Window) in the foreground while the TextBox, which is the target of the input, retains the focus and selection.

Now, I want to add the user's input into the TextBox's Text property just like if he had entered them with a keyboard. That means that certain behaviors should be adhered to, for instance...

  • When the cursor is inbetween the Text, the character should be placed to where the cursor currently resides.
  • When something is selected, the selection should be replaced with the entered character.

Is there anything I can use to do that? The only function I found is TextBoxBase.AppendText , which does not consider the cursor position or selection, but simply appends the entered String to the end of the Text.

Use TextBox.SelectedText , which has both a setter and a getter.

If no text is selected, SelectedText is the zero-length range at the caret position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM