我试图将WPF中的按钮事件单击绑定到视图模型中定义的命令,以下是我现在正在执行的操作:
在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 但它也不起作用。 有什么方法可以让它发挥作用吗?
答案 0 :(得分:2)
<Button
Command="{Binding ChargeModuleDCommand}"
Content="Module A"
Background="Green"
FontWeight="Bold"
/>
如果ModuleAViewModel
是按钮的DataContext
(实际上它本身可能是一大堆蠕虫),那应该可行。