7

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.

SeyedPooya Soofbaf
  • 2,654
  • 2
  • 29
  • 31
Stojdza
  • 445
  • 2
  • 10
  • 32
  • Tried removing the code >Click="btnSaveReminder_Click" – Ralf de Kleine Dec 13 '13 at 11:46
  • yes, when I remove it the command executes well. But I need both, the click event and command to get executed. – Stojdza Dec 13 '13 at 11:49
  • 1
    *I must use click and command together becouse my view and viewmodel are in different projects*... views and view models are often in different projects but that doesn't explain why you need to use both... what are you doing in your `Click` handler? – Sheridan Dec 13 '13 at 11:57
  • Structure of my application doesn't allow me to add reference of my wpf project to my MVVM project, so I can't access my wpf components from mvvm. I use click event only to change the control template of some listbox, and command is used to save some data into db. – Stojdza Dec 13 '13 at 12:01
  • @Stojdza neither should it. The ViewModel should have no knowledge of the View at all. *Does* your ViewModel have a ICommand-derived property called `btnSaveReminder`? Which framework are you using? – Panagiotis Kanavos Dec 13 '13 at 12:04

1 Answers1

24

on the Click event, execute command.

private void btnClick(object sender, RoutedEventArgs e)
{
    var btn = sender as Button;
    btn.Command.Execute(btn.CommandParameter);
}
Rey
  • 3,663
  • 3
  • 32
  • 55
ebattulga
  • 10,774
  • 20
  • 78
  • 116
  • 9
    Better to check `CanExecute()` before calling `Execute()`. Note that if your command might ever unavailable, you should verify that the button is still being enabled/disabled based on the command status. If the command doesn't execute as expected in combination `Click`, the button status may not update correctly either. – Mike Strobel Dec 13 '13 at 15:37