title: Retrieve the selected item from the menuitems of a context menu. I am using WPF, MVVM
body: <p>I have a context Menu on my ListView with FilterBy as one of the Menu items in it. This Menu Item is bound to an observable collection of Menuitems(_childFilterMenuitems coming from an object ContextMenuclass). I want to retrieve the selected item from the submenu of the FilterBy MenuItem and bind to a property called SelectedItem in my MainView Model. </p>
  
  <p>Everything works fine with the relay command and populating the items. However I cannot access the selected item in my main view model. I tried binding the SelectedItem Property(From the MainViewModel) to the Tag Property on the Menuitem. I cannot get this one to work.  I read through several blogs about visual tree and its datacontext and Placement target.Tag, I cant figure out where to include the tag property. Please guide me so that I can establish proper bindings. I am fairly new and this website has been of great help.</p>
  
  <h2>In my MainWindow.xaml</h2>
  
  <pre><code> &lt;ListView.ContextMenu &gt;
     &lt;ContextMenu Name="menuListContext" &gt;
   &lt;MenuItem Header="Reset" Name="menuReset" Command="{Binding ResetCmd}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext}" &gt;
  &lt;MenuItem Header="Filter By"   ItemsSource="{Binding ChildFilterMenuItems}"&gt;  
  &lt;MenuItem.ItemContainerStyle&gt;                                   
     &lt;Style TargetType="{x:Type MenuItem}" &lt;Setter Property="Header" Value="{Binding  Header, Mode=TwoWay}" /&gt;  
     &lt;Setter Property="ItemsSource" Value="{Binding Path=  ChildFilterMenuItems}"/&gt;
     &lt;Setter Property= "Command" Value= "{Binding DataContext.FilterByCmd, RelativeSource   ={RelativeSource AncestorType={x:Type MenuItem}}}"/&gt;
     &lt;Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=DataContext}" /&gt;
     &lt;Setter Property="Tag" Value= "{Binding DataContext.SelectedItem, RelativeSource ={RelativeSource AncestorType=MenuItem}}"/&gt; 
    &lt;/Style&gt;
   &lt;/MenuItem.ItemContainerStyle&gt;
  
    &lt;/ContextMenu &gt;
  &lt;/ListView.ContextMenu&gt;
  </code></pre>
  
  <h2>MainViewModel.xaml</h2>
  
  <pre><code>  public class MainViewModel : ViewModelBase, INotifyPropertyChanged
     {
         /// &lt;summary&gt;
         /// The menu item selected from FilterBy Menu.
         /// &lt;/summary&gt;
         public string SelectedItem {get;set;}
  
       public ObservableCollection&lt;ContextMenuClass&gt; ChildFilterMenuItems
       {
          get
          {
              return _childFilterMenuItems;
  
          }
  
          set
          {
              _childFilterMenuItems = value;
              // Call OnPropertyChanged whenever the property is updated
              OnPropertyChanged("ChildFilterMenuItems");
          }
      }
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the MainViewModel class.
      /// &lt;/summary&gt;      
      public MainViewModel()
      {  _childFilterMenuItems = new ObservableCollection&lt;ContextMenuClass&gt;(); 
         //populates the menuitems for FilterBy
         PopulateFilterBy  
         FilterByCmd = new RelayCommand(() =&gt; FilterByMenu(), () =&gt; true);
      }
      private void FilterByMenu()
      {
          try
          {
            string MenuName = GetExactName(SelectedItem);
           }
  </code></pre>
  
  <h2>ContextMenuClass.cs</h2>
  
  <pre><code>public class ContextMenuClass : INotifyPropertyChanged
  {
      #region Fields
  
      private ObservableCollection&lt;ContextMenuClass&gt; _filterItems;
  
      private string _header;
  
      private bool _isEnabled;
  
      #endregion
  
      #region Properties
  
      /// &lt;summary&gt;
      /// Gets or sets the header.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The header.&lt;/value&gt;
      public string Header
      {
          get
          {
              return _header;
          }
          set
          {
              _header = value; OnPropertyChanged("Header");
          }
      }
  
  
  
      public bool IsEnabled
      {
          get
          {
              return _isEnabled;
          }
          set
          {
               _isEnabled = value; OnPropertyChanged("IsEnabled");
          }
      }
  
      public ObservableCollection&lt;ContextMenuClass&gt; ChildFilterMenuItems
      {
          get
          {
              return (_filterItems = _filterItems ??
               new ObservableCollection&lt;ContextMenuClass&gt;());
          }
      }
      /// &lt;summary&gt;
      /// Gets or sets the SelectedItem.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The header.&lt;/value&gt;
      public object SelectedMenuItem
      {
          get
          {
              return _currentItem = Header;
          }
          set
          {
              _currentItem = value; OnPropertyChanged("SelectedMenuItem");
  
          }
  
      }       
      #endregion
  
      #region INotifyPropertyChanged
      /// &lt;summary&gt;
      /// Occurs when a property value changes.
      /// &lt;/summary&gt;
      public event PropertyChangedEventHandler PropertyChanged;
      private object _currentItem;
  
      /// &lt;summary&gt;
      /// Safely raises the PropertyChanged event.
      /// &lt;/summary&gt;
      /// &lt;param name="property"&gt;The property name.&lt;/param&gt;
  
      protected void OnPropertyChanged(string Property)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(Property));
          }
      }
      #endregion
  </code></pre>