title: Problems when binding The Viewmodel to the contextMenu of a control WPF
body: <p>I am currently having a problem with my project where I pass the binding of the main viewmodel to the contextmenu.</p>
  <p>opening the context menu (using the right mouse button obviously) for the first time gives me this error</p>
  <blockquote>
  <p>System.Windows.Data Error: 40 : BindingExpression path error: '(attached:DependencyObjectAttached.DataContextEx)' property not found on 'object' ''TextBlock' (Name='')'. BindingExpression:Path=PlacementTarget.(attached:DependencyObjectAttached.DataContextEx).QuotationCommandProcessor.ConvertProductCommand; DataItem='ContextMenu' (Name=''); target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')</p>
  </blockquote>
  <p>It may not be super big of a deal since the binding succeeds the second time the context menu opens, but as OCD as I was, I wish to fix this issue.</p>
  <p>So here's what I have</p>
  <ul>
  <li>I have a Page, inside the page is a Datagrid, the Datagrid has a Column whose cell template is the PlacementTarget of the ContextMenu</li>
  <li>The context menu Command binds to a command of the view model of the page</li>
  </ul>
  <p>The implementation i used was through attached property like this</p>
  <pre><code>&lt;DataGrid ItemSource=&quot;{Binding MyItemSources}&quot;&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTemplateColumn CellTemplate=&quot;{StaticResource MyCellStyle}&quot;/&gt;
      &lt;DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  <p>The style looks like this</p>
  <pre><code>&lt;DataTemplate x:Key=&quot;MyCellStyle&quot;&gt;
     &lt;TextBlock Text=&quot;{Binding}&quot; attached:DependencyObjectAttached.DataContextEx=&quot;{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type local:MyPage}}}&quot;&gt;
        &lt;TextBlock.ContextMenu&gt;
           &lt;ContextMenu DataContext=&quot;{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}&quot;&gt;
              &lt;MenuItem Header=&quot;Click Here To Run Command&quot; Command=&quot;{Binding PlacementTarget.(attached:DependencyObjectAttached.DataContextEx).CommandFromTheViewModel, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}&quot; CommandParameter=&quot;{Binding}&quot;/&gt;
           &lt;/ContextMenu&gt;
        &lt;TextBlock.ContextMenu&gt;
     &lt;/TextBlock&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  <p>the DependencyObjectAttached.DataContextEx</p>
  <blockquote>
  <p>attached:DependencyObjectAttached.DataContextEx</p>
  </blockquote>
  <p>is an attached property used to pass the viewmodel to the contextmenu</p>
  <p>I have already tried using the Tag of the placementtarget (the Textblock) and it is working fine, however, I am using the Tag for some other purpose so attached property was the only option i can think of. Any suggestion?</p>