Binding commands to MenuItem

Topics: Prism v4 - WPF 4
Apr 18, 2011 at 9:38 AM

Hi

I am fairly new to both WPF and Prism so forgive me if this question is very basic.

I am binding tool bar buttons using the xaml markup cal:Click.Command as shown below. This is working fine
  <ToolBar>
        <Button Name="_exitButton" ToolTip="Exit" cal:Click.Command="{Binding ExitButtonClicked}">
            <Button.Content>
                <StaticResource ResourceKey="exitImage"/>
            </Button.Content>
        </Button>
    </ToolBar>

My issue is that when I try to do the same for menuitems, I get a compilation error in my xaml. The errors is as follows
"Property 'Command' is not attachable to elements of type 'MenuItem'.
My xaml is as follows
    <Menu>
        <MenuItem Header="File">
            <MenuItem Header="Exit" cal:Click.Command="{Binding ExitButtonClicked}"   />
        </MenuItem>
    </Menu>

I can work around this by using the following syntax (where ExitButtonCommand is ICommand)
    <Menu>
        <MenuItem Header="File">
            <MenuItem Header="Exit" Command="{Binding Path=ExitButtonCommand}" />
        </MenuItem>

I just wanted to know why there was a difference. Why is 'Command' not attachable to 'MenuItem'

 

Thanks

Liam

 

 

Apr 18, 2011 at 2:29 PM

Well for one the WPF architecture includes Commanding so Buttons/MenuItems etc have the Command DependencyProperty already you don't need to add one.  Where as in Silverlight commanding is more or less left out, so you need the interactivity to make click events happen.  So what you were trying to doing and the error is sort of convoluted since you using  WPF and not Silverlight.  In a little bit of irony its surprising actually you didn't get an error on the button you used the property on.

Therefore you don't need the cal:click.Command at all....  This is answer is based off your Topics: Prism v4 - WPF 4

Apr 18, 2011 at 2:45 PM

Ok - that makes sense. Thanks for the explanation.