This project has moved. For the latest updates, please go here.

RelayCommand not firing

Mar 10, 2011 at 11:44 AM

Hi folks,

I am posting here after I had no luck in StackOverflow. 

I am struggling with a problem related to the usage of RelayCommands. Even if the related event seems to be fired, the RelayCommand is not executed.

The RelayCommand is defined in "ShellViewModel", it is a public prop of type ICommand. A "new RelayCommand(() => { throw new NotImplementedException(); })" is assigned to it.

A view named "ShellView" uses the "ShellViewModel" as its DataContext. ShellView uses an instance of UserControl "Keypad", which is composed by several other instances of another UserControl (KeypadButton). These other UserControls do not have their own ViewModel as they use the inherited ShellViewModel. They are just convenient UserControl used to break down the View in several bits. 


// ....

         <Grid Margin="5">
             <Button Name="innerButton" 
                Template="{StaticResource KeypadButtonStyle}"
                ClickMode="Hover" />
// ...


public partial class KeypadButton : UserControl
        // ...

        public delegate void RoutedEventHandler(object sender, RoutedEventArgs e);
        public event RoutedEventHandler Click;

        private void innerButton_Click(object sender, MouseButtonEventArgs e)
            if (Click != null)
                Click(this, new KeypadButtonEventArgs());

        // ...

and this is the related bit of the parent UserControl (Keypad.xaml):

  // ....
    <Grid x:Name="LayoutRoot">
        <controls:KeypadButton x:Name="testBtn">
                <i:EventTrigger EventName="Click">
                    <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding PressStandardKeyCommand}" />

Well, the Click event handler is not null, I can see it is an instance of EventTriggerBase.

The Click(this, args) is executed with no errors, but nothing happens at all.

Does anyone know what the cause of this might be and how to resolve it?

Thanks in advance,



Mar 10, 2011 at 11:53 AM

It looks to me as the button's dataContext is not what you expect (the ShellVieModel), therefore the binding on the command fails. You can check two simple things:

1) look for errors in the Visual Studio output window

2) add a string property in ShellviewModel and bind your button's content to it. That way you'll be sure that the button's dataContext is indeed the desired viewModel.

Mar 10, 2011 at 12:45 PM

@Frenk: Thank you for your answer.

As you suggested, I've looked for errors in the output window but no sign of any binding failure in there.

Also I've checked the dataContext of the button and it is actually set to ShellViewModel.

I really don't know what else could be...


Mar 10, 2011 at 12:56 PM

If you send me a repro (laurent (at) galasoft (dot) ch), I will check it out.



Mar 16, 2011 at 5:50 PM

May be, you have  A button, inside Button?

We have the same problem, till ask our designer, and her says, "ho, i change the ControlTemplate this morning, i put another button inside"..