title: Get owner of context menu in code
body: <p>I have a <code>ContextMenu</code> like this:</p>
  
  <pre><code>&lt;StackPanel Orientation="Horizontal"&gt;
      &lt;StackPanel.ContextMenu&gt;
          &lt;ContextMenu&gt;
              &lt;MenuItem Header="Delete" Click="OnDeleteClicked" /&gt;
          &lt;/ContextMenu&gt;
      &lt;/StackPanel.ContextMenu&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>And I need to get the instance of the <code>StackPanel</code> that owns that <code>ContextMenu</code>. I already tried this:</p>
  
  <pre><code>private void OnDeleteClicked(object sender, System.Windows.RoutedEventArgs e)
  {
      FrameworkElement parent = e.OriginalSource as FrameworkElement;
  
      while (!(parent is StackPanel))
      {               
          parent = (FrameworkElement)LogicalTreeHelper.GetParent(parent);
      }
  }
  </code></pre>
  
  <p>But after getting the <code>ContextMenu</code> Popup parent, it gets null, same with the <code>VisualTreeHelper</code>, it gets to null before getting the <code>StackPanel</code>. Any idea on how to do this?</p>
  
  <p>Thanks!</p>