Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF button click and command doesn't work together MVVM

Tags:

c#

mvvm

wpf

I'm developing some wpf application, using mvvm. I'm trying to use button click event and command together but command never get executed. Also when I use only command without click event it works perfect. Here is the code:

<ControlTemplate x:Key="reminderDataTemplate">          
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25" />
            <RowDefinition Height="150" />
            <RowDefinition Height="20" />
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Content="New reminder:" HorizontalAlignment="Left" />
        <TextBox Text="{Binding Path=ReminderText, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Height="150" Width="200" HorizontalAlignment="Left"/>
        <Button Name="btnSaveReminder" Grid.Row="2" Content="Save" Width="auto" Height="20" HorizontalAlignment="Left" Click="btnSaveReminder_Click"  Command="{Binding Path= btnSaveReminder}"  />
    </Grid>
</ControlTemplate>

Why is this happening?

Just to mention that I must use click and command together becouse my view and viewmodel are in different projects.

UPDATE: Also to say that, when I use click and command together in buttons outside of control template, everything works perfect.

like image 609
Stojdza Avatar asked Dec 13 '13 11:12

Stojdza


1 Answers

on the Click event, execute command.

private void btnClick(object sender, RoutedEventArgs e)
{
    var btn = sender as Button;
    btn.Command.Execute(btn.CommandParameter);
}
like image 156
ebattulga Avatar answered Nov 08 '22 11:11

ebattulga