title: WPF: How to loop through ContextMenu to rearrange the items in it or add a menuitem as a subitem to Menu item
body: <p>I have a ContextMenu with the following items 
  <strong>My current Contextmenu</strong>
  <a href="https://i.stack.imgur.com/8Vqwu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8Vqwu.png" alt="My current Contextmenu"></a></p>
  
  <p>However I want it in this format <strong>My requirement</strong><br>
  <a href="https://i.stack.imgur.com/BKjZh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BKjZh.png" alt="enter image description here"></a></p>
  
  <p>This is how far I have got</p>
  
  <pre><code> foreach (var menuItem in oMenuItemList)
   {
        var cContextMenuItem = menuItem as MenuItem;
        if (cContextMenuItem != null)
        {
            _oContextMenu.Items.Add(menuItem);
            if (cContextMenuItem.Header.ToString() == "1.1")
            {
                 MenuItem newMenuItem2 = new MenuItem();
                 MenuItem newExistMenuItem = (MenuItem)this._oContextMenu.Items[0];
  
                 newExistMenuItem.Items.Add(newMenuItem2);
  
                 _oContextMenu.Items.Add(newExistMenuItem);
              }
          }
     }
   _oContextMenu.ItemsSource = oMenuItemList;
  </code></pre>
  
  <p>Updated code</p>
  
  <pre><code>IEnumerable oMenuItemList = null;
  oMenuItemList = GetContextMenuItems(sMenuItems);
  
  //the function
  private List&lt;object&gt; GetContextMenuItems(string sMenuItems)
  {
   //returns the list
  }
  
  _oContextMenu.ItemsSource = oMenuItemList;
  </code></pre>
  
  <p>After this Im trying to manipulate the _oContextMenu.</p>