gpt4 book ai didi

c# - 如何在 WPF 中手动触发 RelayCommand?

转载 作者:太空宇宙 更新时间:2023-11-03 19:03:08 29 4
gpt4 key购买 nike

我有一个代码片段如下:

XAML

...
<DataGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewKeyDown">
<mvvm:EventToCommand Command="{Binding KeyDownLocationDG}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>

View 模型

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) 中有一个 RawPresentationInput 对象的 KeyPressed 事件。每次触发此事件时,我都会调用 processBarcodeData 委托(delegate)。如果我在 DataGrid 上按下一个键,TestMethod 将立即执行,但我不想那样做,我想要的是让它只能在 DoSomething() 完成后运行。

有人可以帮助我吗?谢谢。

最佳答案

RelayCommand 是一个 ICommand,与所有其他 ICommand 类一样,您只需调用它的 Execute() 函数.

关于c# - 如何在 WPF 中手动触发 RelayCommand?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32730598/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com