c# - 如何使用 Prism 框架将命令绑定(bind)到 WPF 中的按钮

标签 c# wpf xaml mvvm prism

我正在尝试将 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/

相关文章:

c# - 重定向到上一页不起作用

c# - EF 关系 - 由于 list<guid> 是原始的而无法映射

c# - "Object reference not set to an instance of an object"in PresentationFramework with Live Shaping

c# - 在 C# 中将鼠标光标移出屏幕

WPF 中的 C# 和 XAML – 它们如何协同工作?

c# - XAML 中可重用、可重新着色的图标使用什么结构?

c# - 将日期和时间指定为输出文件名

c# - 如何克隆从 C# 中的抽象类派生的类实例的通用列表?

c# - Kinect for Windows 手势识别

c# - WPF - 需要在禁用的 DataGrid 中启用滚动