How do I simulate Text input to a WPF TextBox?

后端 未结 2 420
半阙折子戏
半阙折子戏 2021-02-07 15:05

I want to simulate user input to a WPF TextBox. I want to input a character such that the OnPreviewTextInput event is triggered. I tried setting the Text through the Text proper

2条回答
  •  忘了有多久
    2021-02-07 15:57

    See the answer to How can I programmatically generate keypress events in C#? for a good description of how to simulate input events.

    You could also do:

    TextCompositionManager.StartComposition(
        new TextComposition(InputManager.Current, textBox, "A"));
    

    This will raise the PreviewTextInput event and then raise the TextInput event and change the text.

提交回复
热议问题