将 Enter 解释为选项卡 WPF
我想在整个 WPF 应用程序中将 Enter 键解释为 Tab 键,即在我的应用程序中的任何位置,当用户按 Enter 时,我想聚焦下一个可聚焦控件,但按钮聚焦时除外。 在应用程序生命周期中有什么办法可以做到这一点吗? 谁能给我举个例子吗?
非常感谢!
I want to interpret Enter key as Tab key in whole my WPF application, that is, everywhere in my application when user press Enter I want to focus the next focusable control,except when button is focused. Is there any way to do that in application life circle? Can anyone give me an example?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用我的 EnterKeyTraversal 附加属性代码如果你喜欢。 将其添加到 WPF 窗口上的顶级容器中,其中的所有内容都会将 Enter 视为选项卡:
You can use my EnterKeyTraversal attached property code if you like. Add it to the top-level container on a WPF window and everything inside will treat enter as tab:
根据 Richard Aguirre 的答案,该答案比所选的易于使用的答案更好,恕我直言,您可以通过简单地将 Grid 更改为 UIElement 来使其更加通用。
要在整个项目中更改它,您需要执行以下操作
在 App.xaml.cs 中:
编译。
并做到了。 现在您可以使用类似 tab 的 Enter 功能。
注意:这适用于网格中的元素
Based on Richard Aguirre's answer, which is better than the selected answer for ease of use, imho, you can make this more generic by simply changing the Grid to a UIElement.
To change it in whole project you need to do this
In App.xaml.cs:
Compile.
And done it. Now you can use enter like tab.
Note: This work for elements in the grid
我通过在 XAML 中向某些元素(按钮、组合框或任何我想忽略回车键遍历的元素)添加 FrameworkElement.Tag(其值为 IgnoreEnterKeyTraversal)来解决 woodyiii 的问题。 然后我寻找这个标签& 附加财产中的价值。 就像这样:
I got around woodyiii's issue by adding a FrameworkElement.Tag (whose value is IgnoreEnterKeyTraversal) to certain elements (buttons, comboboxes, or anything I want to ignore the enter key traversal) in my XAML. I then looked for this tag & value in the attached property. Like so:
woodyiii,
UIElement
中有一个名为PredictFocus()
的函数,从名字就知道它的作用,那么你可以检查该元素是否启用,以便移动重点还是不...woodyiii, There is a function in the
UIElement
calledPredictFocus()
which by its name know its function, then you can check if that element is enabled or not so as to move the focus to it or not...这是马特·汉密尔顿的代码,如果有人想知道,因为他的网站显然已关闭:
Here is Matt Hamilton's code, if anyone is wondering since his site is down apparently:
另一种更开/关的实现方法是使用行为:
用法示例:
Another, a more on/off implementation approach would be to use behaviors:
Usage example:
我的解决方案:
用法:
如果您希望将行为仅附加到一个控件而不是布局器中的所有控件,只需将
My solution:
Usage:
If you want the behavior to be attached to only one control instead of all controls within a layouter, simply add the <i:Interaction.Triggers block to that specific control.