Cursor Focus on Textbox in WPF/C#

2019-03-08 15:58发布

I am currently in the process of creating a Onscreen keyboard. I am handling the button click using routedcommands. The issue is that when i click on the button in keyboard panel the focus shifts to the button rather than on the Textbox. The requirement states that a cursor should always appear in the text box to indicate the position where the next character will be inserted. Is there a way i can keep focus on the textbox while button is clicked.

标签: c# wpf textbox
8条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-08 16:26

The way I solved this problem was to set focusable=false to all the buttons/controls on the keyboard. That way you don't lose focused of the current control.

查看更多
放荡不羁爱自由
3楼-- · 2019-03-08 16:27

Get the reference for the specific control(in that case TextBox). After click, in Button_Click method paste this:

Dispatcher.BeginInvoke((ThreadStart)delegate
            {
                control.Focus();
            });
查看更多
登录 后发表回答