在 WindowsForms 我使用这个 samlpe 代码:
textBox.Paste("some text");
WPF中是否有具有相同功能的TextBox方法?或者有一个很好的解决方法?
使用剪贴板类:
textBox1.Text = Clipboard.GetText();
或使用文本框的SelectedText属性:
textBox1.SelectedText = "some text";
public static void Paste(this TextBox textbox, string textToInsert)
{
int caretIndex = textbox.CaretIndex;
string textBoxContent;
if (textbox.SelectedText.Length > 0)
{
textBoxContent = textbox.Text.Remove(caretIndex, textbox.SelectedText.Length);
}
else
{
textBoxContent = textbox.Text;
}
textbox.Text = textBoxContent.Insert(caretIndex, textToInsert);
textbox.CaretIndex = caretIndex + textToInsert.Length;
}