进行“热键"操作在WPF中集中文本框 [英] Make a "hotkey" to focus a TextBox in WPF
问题描述
我正在尝试创建一个热键(即Ctrl + Y),以将焦点更改为文本框.
I am trying to make a hot key (ie Ctrl + Y) that will change the focus to a text box.
我是Delphi的移植者,这使我感到困惑.在Delphi 5中,这样非常容易. (在标签的标题上,您只需要在要用作热键的字母前添加&就可以了.将标签指向TextBox之后,热键就可以使用了.)
I am a transplant from Delp 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.)
对于WPF,我看到的WPF可怕示例涉及调出Win32调用或为每个热键(以及其他此类繁重的实现)发出命令.
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).
我很难相信,在 1999 (Delphi 5)中新增的IDE和Languange版本具有比WPF更好的系统(比WPF)更简单的功能.
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.
推荐答案
所以,我应该一直保持努力.我问这个问题后不久就知道了.
So, I should have kept looking harder. I got it shortly after asking this question.
您执行此操作的方式是创建标签并设置其内容(类似于Content="_Years Of Service"
)
They way you do this is that you create a label and set its Content (something like this Content="_Years Of Service"
)
然后将标签的目标"绑定到文本框. (Target="{Binding ElementName=SomeTextBox}"
)
You then bind the Label's Target to a text box. (Target="{Binding ElementName=SomeTextBox}"
)
现在,如果按Alt + Y,它将把焦点移到SomeTextBox.
Now if you press Alt + Y it will move the focus to SomeTextBox.
这是完整的绑定:
<Label Content="_Label" Target="{Binding ElementName=SomeTextBox}" />
<TextBox Name="SomeTextBox" />
这篇关于进行“热键"操作在WPF中集中文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!