title: Binding WPF ContextMenu MenuItem to UserControl Property vs ViewModel Property
body: <p>I'm struggling to understand what is going on with the ContextMenu. I know it is rendered as a separate window, with a separate visual tree, so we can't use relative binding to bind a command exposed as a property of the user control. e.g. the following <strong>does not</strong> work:</p>
  
  <pre><code>&lt;MenuItem Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=TestCommand}" Header="Test" /&gt;
  </code></pre>
  
  <p>But, if you set the data context of the user control to a view model that exposes a command as a property, the following will work:</p>
  
  <pre><code>&lt;MenuItem Command="{Binding TestCommand}" Header="Test" /&gt;
  </code></pre>
  
  <p>What I don't understand is, how is the ContextMenu inheriting the value of the DataContext if it is not part of the visual tree. I would expect both of these examples to behave the same (i.e. both work or both fail).</p>