繁体   English   中英

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

[英]How to bind command to button in WPF using Prism framework

我正在尝试将 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但它也不起作用。 有什么办法可以让它工作吗?

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

如果ModuleAViewModel是 Button 的DataContext (实际上它本身可能是ModuleAViewModel蠕虫),那应该可行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM