我有一个代码片段,如下所示:
XAML
...
<DataGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewKeyDown">
<mvvm:EventToCommand Command="{Binding KeyDownLocationDG}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
视图模型
public class A
{
public RelayCommand<KeyEventArgs> KeyDownLocationDG { get; set; }
public A()
{
KeyDownLocationDG = new RelayCommand<KeyEventArgs>(TestMethod);
App.processBarcodeData = new App.ProcessBarCodeData((barcode) =>
{
DoSomething();
// then I ONLY want to trigger KeyDownLocationDG command here
});
}
private void TestMethod(KeyEventArgs e)
{
...
}
}
我也有一个MainWindow.xaml文件,在代码隐藏(MainWindow.xaml.cs)中有一个对象KeyPressed
事件RawPresentationInput
。每当此事件触发时,我都会打电话给processBarcodeData
代表。如果按DataGrid上的一个键,TestMethod
将立即执行该操作,但是我不想这样做,我要使它只能在DoSomething()完成后才能运行。
有人能帮我吗?谢谢。
DataGrid.PreviewKeyDown
。事件较早调用Window.KeyPress
。
PreviewKeyDown
在MainWindow中使用事件,或在网格中指定所有操作:
<DataGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewKeyDown">
<mvvm:CallMethodAction Method="DoSomething" />
<mvvm:EventToCommand Command="{Binding KeyDownLocationDG}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>`
在第二种情况下,还应进行设置KeyEventArgs.IsHandled = true;
以防止将事件降级到Window,但可能会产生不良影响
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句