如何在插入位置的WPF文本框中插入文本?我错过了什么?在Win32中,您可以使用CEdit :: ReplaceSel().
它应该像调用Paste()命令一样工作.但我想避免使用剪贴板.
Tar*_*ier 54
只需在插入位置插入文本:
textBox.Text = textBox.Text.Insert(textBox.CaretIndex, "<new text>");
Run Code Online (Sandbox Code Playgroud)
要用新文本替换所选文本:
textBox.SelectedText = "<new text>";
Run Code Online (Sandbox Code Playgroud)
要将文本框滚动到插入位置:
int lineIndex = textBox.GetLineIndexFromCharacterIndex(textBox.CaretIndex);
textBox.ScrollToLine(lineIndex);
Run Code Online (Sandbox Code Playgroud)
小智 11
如果要在插入的文本后移动插入符,则以下代码很有用
textBox.SelectedText = "New Text";
textBox.CaretIndex += textBox.SelectedText.Length;
textBox.SelectionLength = 0;
Run Code Online (Sandbox Code Playgroud)
我自己找到了一个更简单的解决方案:
textBox.SelectedText = "New Text";
textBox.SelectionLength = 0;
Run Code Online (Sandbox Code Playgroud)
然后滚动到眼镜猴所说的位置.