title: WPF: Command on listbox item context menu / menu item does not work even though it does on a button
body: <p>I have a custom RoutedUICommand that works fine when bound to the Command property of a button.  Yet when bound to a ConextMenu MenuItem on a Listbox's item it does not work.  The CanExecute method is not called, and thus the item is left disabled in the context menu.  The CanExecute is called from the button's binding however.</p>
  
  <p>In the repo example below, <code>CrawlCommands.AddCredit</code> is the command.  It is bound to both a button and ListBoxItems.  The Button behaves as expected, but the ListBoxItem does not.  Right clicking on a list box item does show the context menu, and the menu item's header is dervied from the Command properly, but the CanExecute is never called, so it never knows the command is valid and thus stays disabled.</p>
  
  <p>What am I missing?</p>
  
  <pre><code>&lt;UserControl x:Class="CrawlSpace.TestCommands"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           xmlns:local="clr-namespace:CrawlSpace"
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="300" Background="#FF020523"&gt;
  
  
  &lt;UserControl.CommandBindings&gt;
      &lt;CommandBinding Command="local:CrawlCommands.AddCredit" Executed="ExecutedAddCredit" CanExecute="CanExecuteAddCredit"/&gt;
  &lt;/UserControl.CommandBindings&gt;
  
  &lt;Grid&gt;
  
      &lt;Grid.Resources&gt;
          &lt;local:MockData x:Key="TestData"/&gt;
      &lt;/Grid.Resources&gt;
  
      &lt;Grid.Background&gt;
          &lt;SolidColorBrush Color="DarkGray"/&gt;
      &lt;/Grid.Background&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="7*"/&gt;
          &lt;RowDefinition Height="3*"/&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;!-- A button with a command, this one works! --&gt;
  
      &lt;Button x:Name="button" Command="local:CrawlCommands.AddCredit" Content="Add Credit" Margin="10" Grid.Row="1" Width="177"/&gt;
  
      &lt;ListBox x:Name="listBox" Margin="10" Background="LightBlue" ItemsSource="{StaticResource TestData}"&gt;
          &lt;ListBox.Resources&gt;
              &lt;!-- Define a context menu --&gt;
              &lt;ContextMenu x:Key="ItemMenu"&gt;
                  &lt;MenuItem Command="local:CrawlCommands.AddCreditBlock"/&gt;
              &lt;/ContextMenu&gt;
  
              &lt;!-- Sets a context menu for each ListBoxItem in the current ListBox --&gt;
              &lt;Style TargetType="{x:Type ListBoxItem}"&gt;
  
                  &lt;!-- This menu item will not function even though the button verison above does --&gt;
                  &lt;Setter Property="ContextMenu" Value="{StaticResource ItemMenu}"&gt;&lt;/Setter&gt;
              &lt;/Style&gt;
          &lt;/ListBox.Resources&gt;
  
          &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;StackPanel Orientation="Horizontal"&gt;
                      &lt;TextBlock Text="{Binding FirstName}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
      &lt;/ListBox&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>