我正在尝试将 WPF 中的单击按钮事件绑定(bind)到 View 模型中定义的命令,这是我现在的做法:
在 xaml 代码中:
<Grid>
<Button Content="Module A" Background="Green" FontWeight="Bold">
<i:Interaction.Triggers>
<i:EventTrigger EventName="click">
<i:InvokeCommandAction Command="{Binding ChargeModuleDCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
在 ViewModel 类中:
class ModuleAViewModel
{
public DelegateCommand<object> ChargeModuleDCommand { get; set; }
public ModuleAViewModel()
{
ChargeModuleDCommand = new DelegateCommand<object>(LaunchDModule);
}
private void LaunchDModule(object parm)
{
Console.WriteLine("I am in the function");
}
}
但它不起作用。我已尝试按照此问题中的说明进行操作:How to trigger ViewModel command for a specific button events 但它也不起作用。 有什么方法可以让它发挥作用吗?
最佳答案
<Button
Command="{Binding ChargeModuleDCommand}"
Content="Module A"
Background="Green"
FontWeight="Bold"
/>
如果 ModuleAViewModel
是 Button 的 DataContext
,那应该可以工作。
关于c# - 如何使用 Prism 框架将命令绑定(bind)到 WPF 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39980507/