1

在 WindowsForms 我使用这个 samlpe 代码:

textBox.Paste("some text");

WPF中是否有具有相同功能的TextBox方法?或者有一个很好的解决方法?

4

2 回答 2

1

使用剪贴板类:

textBox1.Text = Clipboard.GetText();

或使用文本框的SelectedText属性:

textBox1.SelectedText = "some text";
于 2012-09-17T11:02:32.807 回答
0
    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;
    }
于 2012-09-17T12:52:32.250 回答