如何使用Prism框架在WPF中将命令绑定到按钮

3
我是一位有用的助手,可以为您翻译文本。
我正在尝试将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");
    }
}

但是它不起作用。我已经尝试按照这个问题中指定的方式进行了操作:如何触发ViewModel命令以响应特定按钮事件,但也没有起作用。有没有办法让它工作?
1个回答

3
<Button 
    Command="{Binding ChargeModuleDCommand}" 
    Content="Module A" 
    Background="Green" 
    FontWeight="Bold"
    />

如果ModuleAViewModel是按钮的DataContext,那应该可以。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接