简体   繁体   English

关于光标位置和当前选择,以编程方式更改WPF TextBox的Text

[英]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. 我正在为WPF应用程序实现某种NumericKeypad控件,客户端可以使用该控件方便地通过触摸屏输入文本。

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. 控件本身运行良好,我什至设法在前台运行控件(这是一个窗口),而作为输入目标的TextBox保留了焦点和选择。

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. 现在,我想将用户的输入添加到TextBox的Text属性中,就像用户使用键盘输入它们一样。 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. 我发现的唯一函数是TextBoxBase.AppendText ,它不考虑光标的位置或选择,而只是将输入的String附加到Text的末尾。

Use TextBox.SelectedText , which has both a setter and a getter. 使用TextBox.SelectedText ,它同时具有一个setter和一个getter。

If no text is selected, SelectedText is the zero-length range at the caret position. 如果未选择任何文本,则SelectedText是插入标记位置的零长度范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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