Stojdza
Stojdza

Reputation: 445

WPF button click and command doesn't work together MVVM

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.

Upvotes: 7

Views: 14565

Answers (1)

ebattulga
ebattulga

Reputation: 10981

on the Click event, execute command.

private void btnClick(object sender, RoutedEventArgs e)
{
    var btn = sender as Button;
    btn.Command.Execute(btn.CommandParameter);
}

Upvotes: 24

Related Questions