title: How to force ContextMenu binding custom WPF/XAML behavior?
body: <p><strong>Question:</strong>
  How do we access the bound command assigned to the MenuItem inside our custom behavior?  The ContextMenu isn't a part of the visual tree and doesn't bind until a click event which never occurs due to being suppressed in the custom behavior.</p>
  <p><strong>Purpose</strong>
  I have a custom behavior using Microsoft.Xaml.Behaviors that is intended to only display the context menu if the user clicks on an object in the ListView.  I want to modify the command parameter using the custom behavior while accessing the reference ICommand in the view's markup.</p>
  <p><strong>Code:</strong></p>
  <p><em><strong>Custom Behavior (Microsoft.Xaml.Behaviors):</strong></em></p>
  <pre class="lang-cs prettyprint-override"><code>public class RightClickContextMenuBehavior : Behavior&lt;ListView&gt;
  {
      protected override void OnAttached()
      {
          base.OnAttached();
          AssociatedObject.ContextMenuOpening += AssociatedObject_ContextMenuOpening;
          AssociatedObject.PreviewMouseRightButtonDown += AssociatedObject_PreviewMouseRightButtonDown;
          AssociatedObject.PreviewMouseRightButtonUp += AssociatedObject_PreviewMouseRightButtonUp;
      }
      protected override void OnDetached()
      {
          base.OnDetached();
          AssociatedObject.ContextMenuOpening -= AssociatedObject_ContextMenuOpening;
          AssociatedObject.PreviewMouseRightButtonDown -=
          AssociatedObject_PreviewMouseRightButtonDown;
          AssociatedObject.PreviewMouseRightButtonUp -= AssociatedObject_PreviewMouseRightButtonUp;
      }
      private void AssociatedObject_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
      {
          //Store the click locations so that we can determine drag on mouse move.
          originPoint = e.GetPosition(null);
      
          //Grab the item from the ListView
          var listViewItem = TryFindFromPoint&lt;ListViewItem&gt;((UIElement)sender, 
              e.GetPosition(AssociatedObject));
  
          if (listViewItem == null)
              return;
      
          itemReference = listViewItem;
      }
      private void AssociatedObject_PreviewMouseRightButtonUp(object sender, 
          MouseButtonEventArgs e)
      {
          if (itemReference != null)
          {
              // Only display the context menu if the user clicked on the object
              // inside the listview, not the listview itself.
              var targetPosition = e.GetPosition((UIElement)itemReference);
              HitTestResult hitResult = VisualTreeHelper.HitTest((UIElement)itemReference, 
                  targetPosition);
  
              if (hitResult != null &amp;&amp; sender is ListView)
              {
                  var listView = sender as ListView;
                  
                  //Set the context menu's visibility flag
                  listView.ContextMenu.IsOpen = true; 
      
                  foreach (var item in listView.ContextMenu.Items)
                  {
                      if (item is MenuItem)
                      {
                          var customMemuItem = item as MenuItem;
  
                          //Apply the item the was in our hitbox as context.
                          customMemuItem.CommandParameter = itemReference.DataContext; 
                          /*
                              ISSUE:
                              Bindings here are always null, how do I force the bound 
                              ICommand in the XAML markup to be available here.  I'd like
                              to use the Command binding from the markup, and functionally 
                              apply my DataContext customMemuItem.Command is null
                          */
                      }
                  }
                  e.Handled = true; //Handle the bubble
              }
          }
          else
          {
              if (sender is ListView)
              {
                  //Hide the context menu.
                  var listView = sender as ListView;
                  listView.ContextMenu.IsOpen = false;
                  e.Handled = true;
              }
          }
      }
  }
  </code></pre>
  <p><em><strong>View:</strong></em></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;ListView Grid.Row=&quot;1&quot;
            ItemsSource=&quot;{Binding Path= ItemQueue, IsAsync=True}&quot;
            ScrollViewer.CanContentScroll=&quot;True&quot;
            ScrollViewer.VerticalScrollBarVisibility=&quot;Auto&quot;
            ScrollViewer.HorizontalScrollBarVisibility=&quot;Disabled&quot;&gt;
      &lt;behaviors:Interaction.Behaviors&gt;
          &lt;commandbehaviors:RightClickContextMenuBehavior /&gt;
      &lt;/behaviors:Interaction.Behaviors&gt;
      &lt;ListView.ContextMenu&gt;
          &lt;ContextMenu DataContext=&quot;{Binding PlacementTarget.DataContext,
                                             RelativeSource={RelativeSource Self}}&quot;&gt;
              &lt;MenuItem Header=&quot;Context Menu Command&quot;
                        Command=&quot;{Binding Source={x:Type models:MyViewModel}, 
                        Path=BindingContext.MenuTestCommand}&quot;
                        CommandParameter=&quot;{Binding}&quot;/&gt;
          &lt;/ContextMenu&gt;
      &lt;/ListView.ContextMenu&gt;
      [...]
  &lt;/ListView&gt;
  </code></pre>
  <p><strong>Edit #1:</strong>
  We're using MVVM where ListView is in:</p>
  <pre><code>public partial class MyViewModelView : UserControl
  {
      public DocumentTileManagementView()
      {
          InitializeComponent();
      }
  }
  public class MyViewModel : ViewModelBase
  {
      public MyViewModel()
      {
          MenuTestCommand = new ApplicationRelayCommand(MenuTestCommandBehavior);
      }
  
  
      public ObservableCollection&lt;ObjectViewModel&gt; ItemQueue
      {
          get
          {
              return _ItemQueue;
          }
          set
          {
              this.MutateVerbose(ref _ItemQueue, value, this.RaisePropertyChanged());
          }
      }
      private ObservableCollection&lt;MyDataObject&gt; _ItemQueue= new ObservableCollection&lt;MyDataObject&gt;();
     
      public ICommand MenuTestCommand { get; }
      private async void MenuTestCommandBehavior(object obj)
      {
  
      }
  }
  </code></pre>
  <p><em>I haven't found a way to invoke the binding without allowing the inital right-click event chain on the ListView.</em></p>
  <p>I have a working solution where I allow the first right click event and override the behavior in each subsequent click.  I'd like to invoke whatever is occurring during that first right click in the custom  (Microsoft.Xaml.Behaviors) behavior.</p>