绑定KeyDown事件Silverlight [英] Binding KeyDown Event Silverlight
问题描述
我正在尝试使键绑定在视图模型内引起事件.很久以来,我一直在搜索,但很遗憾,到目前为止还没有遇到任何可行的解决方案.
I am attempting to have a key binding cause an event within the view model. I have been search for a while and have not come across any solutions that have worked thus far, unfortunately.
这基本上就是我要实现的目标:
This is what I am basically attempting to implement:
<i:Interaction.Triggers>
<i:EventTrigger EventName="createNew">
<cal:ActionMessage MethodName="newCustomer" />
</i:EventTrigger>
</i:Interaction.Triggers>
我想要一种提供热键"的方法,以允许用户在视图模型内实现newCustomer事件.到目前为止,它甚至都不会进入视图模型.如果我附加EventName ="KeyDown",则如果按下任何键,效果都很好,但是我试图定位单个键.
I am wanting a way to provide a "hotkey" to allow the user to implement a newCustomer event within the view model. So far it won't even reach into the view model. If I attach the EventName="KeyDown" it works wonderfully if any key is pressed, but I am attempting to target a single key.
我将添加视图模型中的代码如下所示.
I will add that the code behind in the view model looks like this.
public void createNew(object sender, KeyEventArgs e)
{ if (e.Key == Key.F9)
{
addCustomer();
} }
推荐答案
不太确定您使用的控件是什么,但是出于示例目的,我将使用文本框... 尚不确定这是否会有所帮助,但我希望这样做:
Not too sure what control you are using, but for example purposes I'll use a text-box... Not entirely sure if this will help even, but I hope it does:
视图:
<TextBox x:Name="MyTextBox" TextAlignment="Left" FontSize="10" Width="240" cal:Message.Attach="[Event KeyUp]=[Action ExecuteAction($executionContext)]"/>
ViewModel:
*出于示例目的,我将"Enter"用作您要在其上执行操作的单键.
The ViewModel:
* For example purposes I'll use "Enter" as the single key on which you want the actions performed.
public void ExecuteAction(ActionExecutionContext context)
{
var eventArgs = (KeyEventArgs)context.EventArgs;
if (eventArgs.Key != Key.Enter) return;
//Execute Actions...
}
这应该在您想要的控件上的每次按键上触发代码,如果按键不等于您指定的值,那么它将什么也不会做...
This ought to fire code on each key-press on the control you want, if the key is not equal to whatever you specify, it won't do anything...
如果我不愿意,也许我只是不正确地理解问题:D
If I'm waaay off, maybe I just don't understand the issue correctly :D
这篇关于绑定KeyDown事件Silverlight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!