Make a “hotkey” to focus a TextBox in WPF

≡放荡痞女 提交于 2020-01-14 07:34:08

问题


I am trying to make a hot key (ie Ctrl + Y) that will change the focus to a text box.

I am a transplant from Delphi, and this is confusing me. In Delphi 5 this was so, so easy. (On the caption of the label you could just add an & before the letter you want to make the hot key. After you point the label at the TextBox the hotkey would work.)

For WPF, I am seeing horrific examples in WPF involving calling out to Win32 calls or making a command for each hotkey (and other such heavy implementations).

I find it hard to believe that an IDE and Languange version that was new in 1999 (Delphi 5) has a better system (than WPF) for something as simple as Hotkeys.

Surely I am missing something. If you know, please tell me what it is.


回答1:


So, I should have kept looking harder. I got it shortly after asking this question.

They way you do this is that you create a label and set its Content (something like this Content="_Years Of Service")

You then bind the Label's Target to a text box. (Target="{Binding ElementName=SomeTextBox}")

Now if you press Alt + Y it will move the focus to SomeTextBox.

Here is the full binding:

<Label Content="_Label" Target="{Binding ElementName=SomeTextBox}" />
<TextBox Name="SomeTextBox" />


来源:https://stackoverflow.com/questions/10452462/make-a-hotkey-to-focus-a-textbox-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!