title: WPF UserControl - Binding parent window as command parameter to ContextMenu MenuItem
body: <p>I have a <code>UserControl</code> with <code>Button</code> inside which opens a <code>ContextMenu</code> when left clicked. I'm trying to  pass <code>UserControl</code>'s parent <code>Window</code> as a parameter to <code>ContextMenu</code> item's command to close that window, but with no avail. I've tried everything with <code>RelativeSource</code> and <code>PlacementTarget</code>, but parameter is always null. I'm aware that <code>ContextMenu</code> is not part of parent window's <code>VisualTree</code>. I'm currently stuck with this approach, but it is not working.</p>
  
  <pre><code>&lt;Grid x:Name="LayoutRoot"&gt;
          &lt;Button 
              HorizontalAlignment="Left" 
              Margin="0" 
              Style="{DynamicResource ButtonStyle1}" 
              VerticalAlignment="Top" 
              Width="120" 
              Height="25" 
              Content="Dashboard Menu" 
              TextElement.FontWeight="Bold" 
              Foreground="AliceBlue"             
              &gt;
  
              &lt;!--Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={ x:Type Window}}}"--&gt;
              &lt;Button.ContextMenu&gt;
                  &lt;ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" &gt;
                      &lt;MenuItem Header="Open Log Viewer" Command="{StaticResource openLogViewer}" /&gt;
                      &lt;Separator /&gt;
                      &lt;MenuItem Header="Exit" Command="{StaticResource exit}" CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=Window}}"/&gt;
                  &lt;/ContextMenu&gt;
              &lt;/Button.ContextMenu&gt;
          &lt;/Button&gt;
      &lt;/Grid&gt;
  </code></pre>
  
  <p>Command is a Referenced command defined in <code>UserControl.Resources</code>:</p>
  
  <pre><code>&lt;my:CommandReference x:Key="exit" Command="{Binding Exit}" /&gt;
  </code></pre>
  
  <p>and it's Execute part is triggered but parameter is always null. So, my question is, what is the right way to bind parent window as <code>CommandParameter</code> of <code>MenuItem</code>. Any help is appreciated, because this thing is bothering me for almost two days.</p>