title: Command Binding not working on a context sensitive menu?
body: <p>I have an user control (ex: UserCtrlClass)with a tree view inside it </p>
  
  <p>I have View model (ex: OBJViewModel) class for represent the actual items/data display on the tree view</p>
  
  <p>Next I have a Tree View Model (ex: TreeViewModel), which has a list of OBJViewModel objects</p>
  
  <p>Now in the code behind file of the user control, I have instantiated the tree view model class and set as the data context of the user control class</p>
  
  <p>I need a context sensitive menu, which i need to display only when I right click on a specific item in the tree, so I have handled the  right click event of the user control class and did the work there</p>
  
  <p>But the commands are not working, The commands are derived from I command and instantiated in TreeViewModel class. i tried to debug my Command.execute was never hit! Any help would be appreciated as I am being a newbie to .net and wpf</p>
  
  <p>TreeViewModel class</p>
  
  <pre><code>&lt;UserControl Name="PFDBUserCtrl" x:Class="BFSimMaster.BFSMTreeview"
           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:BFSimMaster.ViewModel"
           xmlns:cmd="clr-namespace:BFSimMaster.Commands"
           mc:Ignorable="d" 
           d:DesignHeight="66" d:DesignWidth="300"&gt;
  &lt;UserControl.Resources&gt;
      &lt;!--cmd:ActivateProjectCmd x:Key="CMDActivateProject"/--&gt;
      &lt;!--cmd:DeActivateProjectCmd x:Key="CMDDeActivateProject"/--&gt;
  &lt;/UserControl.Resources&gt;
  &lt;DockPanel&gt;
      &lt;!-- PF Object Browser TREE --&gt;
      &lt;TreeView Name="PFDataBrowser" ItemsSource="{Binding LevelOnePFObjects}" &gt;  
          &lt;TreeView.Resources&gt;
              &lt;ContextMenu x:Key ="ProjectMenu"  StaysOpen="true" &gt;
                  &lt;!-- Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}--&gt;
                  &lt;!--MenuItem Header="Activate" Command="{Binding Source={StaticResource CMDActivateProject}}" CommandParameter="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType=ContextMenu}}"/--&gt;
                  &lt;MenuItem Header="Activate" Command="{Binding DataContext.CMDActivateProject, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" CommandParameter="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType=ContextMenu}}"/&gt;
                  &lt;MenuItem Header="Deactivate" Command="{Binding Source=TVViewModel, Path=CMDDeActivateProject}" CommandParameter="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType=ContextMenu}}"/&gt;
              &lt;/ContextMenu&gt;
          &lt;/TreeView.Resources&gt;
          &lt;TreeView.ItemContainerStyle&gt;
              &lt;!-- This Style binds a TreeViewItem to a PFObject View Model.--&gt;                
              &lt;Style TargetType="{x:Type TreeViewItem}"&gt;                    
                  &lt;EventSetter Event="MouseRightButtonDown" Handler="OnRightButtonDown"/&gt;
                  &lt;Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /&gt;
                  &lt;Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /&gt;
                  &lt;Setter Property="FontWeight" Value="Normal" /&gt;
  
                  &lt;Style.Triggers&gt;
                      &lt;Trigger Property="IsSelected" Value="True"&gt;
                          &lt;Setter Property="FontWeight" Value="Bold" /&gt;
                      &lt;/Trigger&gt;                        
                  &lt;/Style.Triggers&gt;
  
              &lt;/Style&gt;
          &lt;/TreeView.ItemContainerStyle&gt;
  
          &lt;TreeView.ItemTemplate&gt;
              &lt;HierarchicalDataTemplate ItemsSource="{Binding Children}"&gt;
                  &lt;TextBlock Text="{Binding Name}" /&gt;
              &lt;/HierarchicalDataTemplate&gt;
          &lt;/TreeView.ItemTemplate&gt;
      &lt;/TreeView&gt;
  &lt;/DockPanel&gt;
  </code></pre>
  
  <p></p>
  
  <p>Code behind class</p>
  
  <pre><code>using System;
  namespace BFSimMaster
  {
  
  public partial class BFSMTreeview : UserControl
  {
  
      readonly TreeViewItemViewModel mViewModelPFObjBrowserTree;  
      public BFSMTreeview()
      {
          InitializeComponent();
  
          WApplication appPF = PFAPIUtils.APIInstance.GetApplication();
          WDataObject User = appPF.GetCurrentUser();
  
  
  
          // Get raw objects - tree data from a PF database.
          //BFPFDataObject userdb = new BFPFDataObject(User,false,"*.IntPrj");
          BFPFDataObject userdb = new BFPFDataObject(User, true);
  
          // Create UI-friendly wrappers around the 
          // raw data objects (i.e. the view-model).
          mViewModelPFObjBrowserTree = new TreeViewItemViewModel(userdb);
  
          // Let the UI bind to the view-model.
          base.DataContext = mViewModelPFObjBrowserTree;
  
      }
      public TreeViewItemViewModel TVViewModel
      {
          get { return mViewModelPFObjBrowserTree; }
      }
  
      private void OnRightButtonDown(object sender, MouseButtonEventArgs e)
      {
          //MessageBox.Show("Right Clicked on tree view");
          if (sender is TreeViewItem)
          {
              e.Handled = true;
              (sender as TreeViewItem).IsSelected = true;
  
              string strObjectType = ((sender as TreeViewItem).Header as PFObjectViewModel).PFDataObject.mThisPFObject.GetClassName().GetString();
              switch (strObjectType)
              {
                  case "IntPrj":
                      (sender as TreeViewItem).ContextMenu = PFDataBrowser.Resources["ProjectMenu"] as System.Windows.Controls.ContextMenu;
                      (sender as TreeViewItem).ContextMenu.PlacementTarget = (sender as TreeViewItem); 
                      break;
                  case "Folder":
                      (sender as TreeViewItem).ContextMenu = PFDataBrowser.Resources["ProjectMenu"] as System.Windows.Controls.ContextMenu;
                      break;
              }
  
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>the TreeViewModel Class</p>
  
  <pre><code>using System;
  namespace BFSimMaster.ViewModel
  {
  
  public class TreeViewItemViewModel 
  {
      #region Data
  
      readonly ReadOnlyCollection&lt;PFObjectViewModel&gt; mLevelOnePFObjects;
      readonly PFObjectViewModel mRootOfPFObjects;
  
      #endregion // Data
  
      #region Constructor
  
  
      public TreeViewItemViewModel(BFPFDataObject rootOfPFObjectsA)
      {
          this.CMDActivateProject = new ActivateProjectCmd();
          this.CMDDeActivateProject = new DeActivateProjectCmd();
          mRootOfPFObjects = new PFObjectViewModel(rootOfPFObjectsA);
  
          mLevelOnePFObjects = new ReadOnlyCollection&lt;PFObjectViewModel&gt;(
              new PFObjectViewModel[] 
              { 
                  mRootOfPFObjects 
              });            
      }
  
      #endregion // Constructor
      public ICommand CMDActivateProject { get; set; }
      public ICommand CMDDeActivateProject { get; set; }
  
      public ReadOnlyCollection&lt;PFObjectViewModel&gt; LevelOnePFObjects
      {
          get { return mLevelOnePFObjects; }
      }       
  
  }
  }
  </code></pre>