WPF:选项卡的键绑定,吞下选项卡并且不传递它
我有一个文本框,里面有:
问题是它吞掉了 Tab,并且不会跳到下一个控件。 如何捕获文本框的 Tab 键并仍然保留 Tab 键切换到 Tab 键顺序中的下一个控件? 编辑:我也在使用 MVVM 并且 MyCommand 在 ViewModel 代码中,所以这就是我需要重新抛出选项卡的地方。
I've got a textbox where I have this:<KeyBinding Command="{Binding MyCommand}" Key="Tab"/>
Problem is it swallows the Tab and doesn't tab to the next control.
How can I trap the Tab for the textbox and still preserve tabbing to the next control in the tab order?
Edit: I'm also using MVVM and MyCommand is in the ViewModel code, so that's where I need to re-throw the Tab.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很容易实现,只是不要为此使用 KeyBinding 即可。处理 TextBox 的 OnKeyDown 事件:
然后在代码隐藏中,每当按下 Tab 时执行命令。与 KeyBinding 不同,这不会吞噬 TextInput 事件,因此它应该可以工作。
It's easy to achieve, just don't use KeyBinding for this. Handle your TextBox's OnKeyDown event:
Then on the code-behind, execute your command whenever Tab is pressed. Unlike KeyBinding, this won't swallow the TextInput event so it should work.
鉴于您的问题作为纯粹的 XAML 解决方案,我找不到将焦点设置为控件的方法。
我选择创建一个 attacted 属性,然后通过绑定将焦点设置为与 ViewModel 中的 KeyBinding 关联的命令中的下一个控件。
这是视图:
这是 ViewModel:
这是定义我在以下内容中找到的附加属性的代码 答案。
I cannot find a way to set focus to a control given your question as a purely XAML solution.
I choose to create an attacted property and then through binding set the focus to next control from the Command associated with your KeyBinding in the ViewModel.
Here is the View:
Here is the ViewModel:
Here is the code to define the attached property that I found in the following answer.
为什么不在命令处理程序中使用此代码?
这基本上就是“Tab”所做的事情,所以如果您也这样做,就可以开始了。 (当然,如果你有一个带有 Shift-Tab 的命令,请反转方向。
我实际上将其包装到一个扩展方法中,就像这样
......意味着先前的代码变得更加简单,就像这样......
并且如果您不知道当前关注的控件是什么,您可以这样做...
希望这会有所帮助!如果您投票支持我或将其标记为已接受,我们将不胜感激!
Why don't you just use this code in your command handler?
That's basically what 'Tab' does, so if you do the same, you're good to go. (Of course reverse the direction if you have a command with Shift-Tab.
I actually wrapped this into an extension method like so...
...meaning the prior code becomes even simpler, like this...
...and if you don't know what the currently focused control is, you can just do this...
Hope this helps! If so, much appreciated if you vote me up or mark it as accepted!
遇到了同样的问题,遇到了这个线程,花了我一段时间才找到最好的答案。参考:在特定键上使用 EventTrigger
定义此类:
文本框的 xaml:
在窗口或用户控件根标记中包含以下属性:
Had the same problem, came across this thread and took me a while to find the best answer. Reference: Use EventTrigger on a specific key
Define this class:
The xaml for your text box:
In your window or user control root tag include these attributes: