title: Setting the DataContext for a ContextMenu from a ListView's ItemContainer?
body: <p>I am using the mvvm pattern and I am having a hard time figuring out how to set the DataContext on a ContextMenu from inside a ListView’s ItemContainerStyle. </p>
  
  <p>I also do not understand why the ListView.ContextMenu and the ListView’s GridView.ColumnHeaderContextMenu can see the properties and commands from my view model, but the ContextMenu inside the ListView.ItemContainerStyle cannot.</p>
  
  <p><strong>Error</strong></p>
  
  <p>System.Windows.Data Error: 40 : BindingExpression path error: 'AddMenuItem' property not found on 'object' ''Currency' (HashCode=43406546)'. BindingExpression:Path=AddMenuItem; DataItem='Currency' (HashCode=43406546); target element is 'ContextMenu' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')</p>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;!-- Removed styles for clarity. --&gt;
  &lt;UserControl&gt;
  
  &lt;!-- Add ElementSpy to the UserControl’s rsources --&gt;
  &lt;UserControl.Resources&gt;
      &lt;framework:ElementSpy x:Key="spy" /&gt;
  &lt;/UserControl.Resources&gt;
  
  &lt;ListView ItemsSource="{Binding Currency}"&gt;
  
      &lt;ListView.ItemContainerStyle&gt;
          &lt;Style TargetType="ListViewItem"&gt;
              &lt;Setter Property="ContextMenu"&gt;
                  &lt;Setter.Value&gt;
                      &lt;!-- 'AddMenuItem' property not found on 'object' 'Currency' --&gt;
                      &lt;!-- ContextMenu ItemsSource="{Binding AddMenuItem}" / --&gt;
  
                      &lt;!-- Use the ElementSpy resource --&gt;
                      &lt;ContextMenu ItemsSource="{Binding Source={StaticResource spy}, Path=Element.DataContext.AddMenuItem}" /&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
      &lt;/ListView.ItemContainerStyle&gt;
  
      &lt;ListView.ContextMenu&gt;
          &lt;!-- Works --&gt;
          &lt;ContextMenu ItemsSource="{Binding EditMenuItem}" /&gt;
      &lt;/ListView.ContextMenu&gt;
  
      &lt;ListView.View&gt;
          &lt;GridView&gt;
              &lt;GridView.ColumnHeaderContextMenu&gt;
                  &lt;!-- Works --&gt;
                  &lt;ContextMenu ItemsSource="{Binding SortMenuItem}" /&gt;
              &lt;/GridView.ColumnHeaderContextMenu&gt;
  
              &lt;GridViewColumn Header="Code"
                              DisplayMemberBinding="{Binding Path=Code}" /&gt;
  
              &lt;GridViewColumn Header="Description"
                              DisplayMemberBinding="{Binding Path=Description}" /&gt;
  
              &lt;GridViewColumn Header="Exchange Rate"
                              DisplayMemberBinding="{Binding Path=ExchangeRate}" /&gt;
          &lt;/GridView&gt;
  
      &lt;/ListView.View&gt;
  
  &lt;/ListView&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>Code Behind</strong></p>
  
  <pre><code>[Export(ViewNames.CurrencyMasterView, typeof(IMasterView))]
  [PartCreationPolicy(CreationPolicy.Shared)]
  public partial class CurrencyMasterView
      : UserControl, IMasterView
  {
      public CurrencyMasterView()
      {
          InitializeComponent();
      }
  
      [Import]
      private MasterViewModel ViewModel
      {
          set
          {
              this.DataContext = value;
          }
      }
  }
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>[Export(typeof(MasterViewModel))]
  [PartCreationPolicy(CreationPolicy.NonShared)]
  public class MasterViewModel
      : ViewModelBase
  {
      [ImportingConstructor]
      public MasterViewModel(IGeneralController generalController, IRegionManager regionManager)
      {
      }
  
      public ObservableCollection&lt;Currency&gt; Currency
      {
          get
          {
              return this.currency;
          }
  
          set
          {
              if (this.currency != value)
              {
                  this.currency = value;
                  this.RaisePropertyChanged(() =&gt; this.Currency);
              }
          }
      }
  
      public List&lt;MenuItemMvvm&gt; SortMenuItem
      {
          get
          {
              return this.CreateSortMenuItem();
          }
      }
  
      public List&lt;MenuItemMvvm&gt; EditMenuItem
      {
          get
          {
              return this.CreateEditMenuItem();
          }
      }
  
      public List&lt;MenuItemMvvm&gt; AddMenuItem
      {
          get
          {
              return this.CreateAddMenuItem();
          }
      }
  
      private List&lt;MenuItemMvvm&gt; CreateEditMenuItem()
      {
          var menu = new List&lt;MenuItemMvvm&gt;();
  
          menu.Add(new MenuItemMvvm("_Edit")
          {
              Command = this.EditCommand,
              Icon = new Image
              {
                  Source = new BitmapImage(new Uri("pack://application:,,,/POS.Modules.Core;component/Resources/Images/16X16/Edit.png"))
              }
          });
  
          menu.Add(new MenuItemMvvm("_Duplicate")
          {
              Command = this.DuplicateCommand,
              Icon = new Image
              {
                  Source = new BitmapImage(new Uri("pack://application:,,,/POS.Modules.Core;component/Resources/Images/16X16/Copy.png"))
              }
          });
  
          menu.Add(new MenuItemMvvm("_Delete")
          {
              Command = this.DeleteCommand,
              Icon = new Image
              {
                  Source = new BitmapImage(new Uri("pack://application:,,,/POS.Modules.Core;component/Resources/Images/16X16/Delete.png"))
              }
          });
  
  
          return menu;
      }
  
      // Other methods removed for clarity
  }
  </code></pre>