title: WPF Contextmenu itemtemplate commandParameter binding returns null
body: <p>I have a problem. I am trying to create a context menu with databinding in wpf. The context menu items would are bound to an observable collection of objects. The population of context menu is fine - however, I want to add the command to it. </p>
  
  <p>The way I am using it is:</p>
  
  <p>in XAML</p>
  
  <pre><code> &lt;Grid.Resources&gt;
              &lt;local:RestoreCommand x:Key="RestoreCommand" /&gt;
              &lt;local:ShowBalloonCommand x:Key="BaloonCommand" /&gt;
              &lt;local:StartTaskCommand x:Key="StartTaskCommand" /&gt;
          &lt;/Grid.Resources&gt;
  
  &lt;ContextMenu ItemsSource="{Binding}" Name="taskBarContextMenu"&gt;
                      &lt;ContextMenu.ItemContainerStyle&gt;
                          &lt;Style TargetType="MenuItem"&gt;
                              &lt;Setter Property="Command" Value="{StaticResource StartTaskCommand}"/&gt;
                              &lt;Setter Property="CommandParameter" Value="{Binding mainWindow}"/&gt;
                          &lt;/Style&gt;
                      &lt;/ContextMenu.ItemContainerStyle&gt;
  </code></pre>
  
  <p>and </p>
  
  <pre><code>&lt;TextBlock TextWrapping="Wrap" Text="{Binding Name}" Grid.Column="0"/&gt;
                                      &lt;TextBlock TextWrapping="Wrap" Text="{Binding TimeElapsed, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2"/&gt;
  </code></pre>
  
  <p>and in C#:</p>
  
  <pre><code>taskbarIcon.ContextMenu.ItemsSource = TasksList;
  </code></pre>
  
  <p>And I have a class for the command</p>
  
  <pre><code>public class StartTaskCommand : ICommand
      {
          public void Execute(object parameter)
          {
              //THE PARAMETER IS ALWAYS NULL
              var window = parameter as MainWindow;
              if (window != null)
              {
  
              }
          }
  
          public bool CanExecute(object parameter)
          {
              return true;
          }
  
          public event EventHandler CanExecuteChanged;
      } 
  </code></pre>
  
  <p>I have it set up simiarly for other two commands and it works fine. Anything I try to add as the parameter is always null - be it taskBarContextMenu, or mainWindow or menuItem...
  Any ideas welcome.</p>
  
  <p>====</p>
  
  <p>I tried a following solution as suggested, but the command parameter is still null:</p>
  
  <pre><code> &lt;tb:TaskbarIcon Name="taskbarIcon"
              DoubleClickCommand="{StaticResource RestoreCommand}"
              DoubleClickCommandParameter="{Binding ElementName=mainWindow}"
  
              LeftClickCommand="{StaticResource BaloonCommand}"
              LeftClickCommandParameter="{Binding ElementName=mainWindow}"
              IconSource=".\256px-Out_of_date_clock_icon.ico"
                          MenuActivation="RightClick"
                          Tag="{Binding ElementName=mainWindow}"
                          &gt;
  
  
  
  &lt;tb:TaskbarIcon.ContextMenu &gt;&lt;ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}" Tag="{Binding}" Name="taskBarContextMenu"&gt;
              &lt;ContextMenu.ItemContainerStyle&gt;
                  &lt;Style TargetType="MenuItem"&gt;
                      &lt;Setter Property="Command" Value="{StaticResource StartTaskCommand}"/&gt;
                      &lt;Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.DataContext}"/&gt;
                  &lt;/Style&gt;
              &lt;/ContextMenu.ItemContainerStyle&gt;
  </code></pre>