使用键盘上的 Enter 键以及鼠标单击将按钮绑定到命令
我有一个使用 MVVM Light 构建的 Silverkight 4 应用程序。我有各种带有按钮的视图,这些按钮绑定到视图模型中的命令。
一切工作正常,当您单击按钮时,命令会触发,并且无论调用什么都可以工作。
我想要改变的只是用户不必使用鼠标按下按钮来调用命令,我希望他们可以选择按键盘上的回车键。
我想很简单,但目前我陷入困境,无法找到任何有关如何完成此操作的信息。任何想法请。
I have a Silverlkight 4 application built with MVVM Light. I have various views with buttons on that are bound to commands in the viewmodels.
Everything works fine, when you click on a button, the command fires and whatever was called works.
All I want to change is instead of the user having to use to mouse to press the button to call the command I would like them to have the option of pressing return on the keyboard.
Simple I thought but at the moment I am stuck and cannot find any info of how to accomplish this. Any ideas please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不在控件上添加一个 KeyUp 事件 - 并在后面的代码中处理“Enter”键以在视图模型中触发该事件?
EG:
在 XAML 中:
在后面的代码中:
其中 GlobalViewModelLocator 引用一个静态类,该类保存对视图中使用的视图模型的引用。
Why dont you add a KeyUp event on the control - and handle the "Enter" key in the code behind to fire off the event in the view model?
EG:
In XAML:
In the CODE BEHIND:
Where GlobalViewModelLocator refers to a static class which holds references to the view models used in the views.
谢谢维克斯的回复。我可以看到这是如何运作的。我已经使用后面的代码对其进行了排序。
在 xaml 中,我将 keydown 事件添加到我所在的控件的属性中,在我的例子中是一个网格,但可以是列表框、文本框或其他任何内容。
然后在后面的代码中我添加了
这对我有用
Thanks for the reply Vixen. I could see how this would work. I have already sorted it by using the code behind.
In the xaml I add a keydown event to the property of the control I was in which in my case was a grid but could be a listbox, textbox or whatever.
Then in the code behind I added
This worked for me