NicolasR NicolasR - 1 month ago 17
C# Question

How to bind command to button in WPF using Prism framework

I am trying to bind a click on button event in WPF to a command defined in a View Model, here is how I am doing that for now :

In the xaml code :

<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>


and in the ViewModel class :

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");
}
}


but it does not work. I've tried to do it as specified in this question : How to trigger ViewModel command for a specific button events
but it does not work either.
Is there any way that I can make it work ?

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

If ModuleAViewModel is the Button's DataContext, that should work.

Comments