title: Binding to a ContentPresenter's visual elements/children from outside
body: <p>First a brief "abstracted" short version of my problem.
  Probably not needed to discuss a solution, but below some further "optional" infos of the real underlying problem I'm having, just to understand the context.</p>
  
  <p>So: I have a ContentPresenter using a DataTemplate to generate its layout for bound items.
  Now, outside of this contentpresenter, I'm trying to bind within an Element by name within that content presenter.</p>
  
  <p>Assume the following Pseudo-XAML (MainTextBlock's binding won't work in practice):</p>
  
  <pre><code>   &lt;TextBlock Text="{Binding Text, ElementName=MyTextblock, Source = ???}" DataContext="{x:Reference TheContentPresenter}" x:Name="MainTextblock"/&gt;
  
      &lt;ContentPresenter Content="{Binding SomeItem}" x:Name="TheContentPresenter"&gt;
          &lt;ContentPresenter.ContentTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;TextBlock x:Name="MyTextblock" Text="Test"/&gt;
              &lt;/DataTemplate&gt;
          &lt;/ContentPresenter.ContentTemplate&gt;
      &lt;/ContentPresenter&gt;
  </code></pre>
  
  <p>!! Please assume that the DataContext of MainTextblock MUST be (a reference to) TheContentPresenter !!</p>
  
  <p>Given that assumption, how can I make the binding on MainTextblock work?</p>
  
  <p>I can't bind to the ContentPresenter's Content property, because that contains the bound element (e.g. SomeItem), not its visual representation.
  Unfortunately, ContentPresenter doesn't seem to have any property representing its Visual Tree / Visual Children.</p>
  
  <p>Is there any way to do this?</p>
  
  <hr>
  
  <p>Now what do I really need this for? Feel free to skip reading this, it shouldn't be needed to discuss a solution of above's problem I believe.</p>
  
  <p>I'm writing a behavior that adds customizable filters to a DataGrid:</p>
  
  <pre><code>&lt;DataGrid AutoGenerateColumns="False"&gt;
  
          &lt;i:Interaction.Behaviors&gt;
              &lt;filter:FilterBehavior&gt;
                  &lt;filter:StringFilter Column="{x:Reference FirstCol}" Binding="{Binding DataContext.Value1}"/&gt;
                  &lt;filter:StringFilter Column="{x:Reference SecondCol}" Binding="{??????? bind to Content -&gt; Visual Children -&gt; Element With Name "MyTextBlock" -&gt; Property "Text"}"/&gt;
              &lt;/filter:FilterBehavior&gt;
          &lt;/i:Interaction.Behaviors&gt;
  
  
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn x:Name="FirstCol" Header="Test" Binding="{Binding Value1}"/&gt;
              &lt;DataGridTemplateColumn x:Name="SecondCol" Header="Test 2"&gt;
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBlock x:Name="MyTextblock" Text="{Binding Value2}"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
              &lt;/DataGridTemplateColumn&gt;
          &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>"FilterBehavior" contains individual filters for each column, e.g. the first of them would be a filter that allows to search text within whatever column its bound to (FirstCol in this case), and hides columns where that text doesn't appear.</p>
  
  <p>Now, Binding is the interesting part. The Binding property is of type BindingBase (so the binding is "deferred"). It's intended to define the value which is used for filtering.
  When filtering is supposed to occur, each filter loops through all DataGridCells of the column its bound to. For each DataGridCell, it sets the Binding's DataContext to the respective DataGridCell, and evaluates the binding.</p>
  
  <p>So, the StringFilter would loop through each DataGridCell in FirstCol.
  For Each of them, it would retreive BindingBase "Binding" (i.e. {Binding DataContext.Value1}), set its DataContext to the DataGridCell, and evaluate this.
  So in that case, it would bind to WpfGridCell.DataContext.Value1, or in other words to the Value1 property of the item the DataGridCell contains.
  Later on, it will check if these evaluated items match the String that the user entered for filtering.</p>
  
  <p>This works just fine.</p>
  
  <p>However, I'm having trouble when trying to bind to the DataGridCell's visual content, as in the case of the second StringFilter with Column="{x:Reference SecondCol}".
  SecondCol is a DataGridTemplateColumn. It's cells content will be a ContentPresenter, whose template is DataGridTemplateColumn.CellTemplate, and whose Content is the element that the cell contains.</p>
  
  <p>And this is where we get back to my simplified version from above. I now need to evaluate "Binding" with DataContext = DataGridCell, and somehow come up with a binding that let's me bind to the visual elements of the ContentPresenter that is given in DataGridCell.Content.</p>
  
  <p>Thanks!</p>