title: defining bindings for elements in a collection in xaml wont work
body: <p>i have a simple Control with a collection of elements. I want to <strong>add elements in xaml</strong> and <strong>bind to the element</strong>.</p>
  
  <p>However when i bind to Bar.Value in xaml it never works. Minimal example:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[ContentProperty("Bars")]
  public class FooControl : Control
  {
      private ObservableCollection&lt;IBar&gt; _bars = new ObservableCollection&lt;IBar&gt;();
      public ObservableCollection&lt;IBar&gt; Bars { get { return _bars; } }
  }
  
  public interface IBar
  {
      string Value { get; }
  }
  
  public class Bar : DependencyObject, IBar
  {
      public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(Bar), new PropertyMetadata("&lt;not set&gt;"));
      public string Value
      {
          get { return (string)GetValue(ValueProperty); }
          set { SetValue(ValueProperty, value); }
      }
  }
  </code></pre>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Window x:Class="WpfTestApplication.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:this="clr-namespace:WpfTestApplication"
          xmlns:sys="clr-namespace:System;assembly=mscorlib"
          Title="MainWindow" Height="200" Width="1000"&gt;
  
      &lt;Window.Resources&gt;
          &lt;Style TargetType="this:FooControl"&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="this:FooControl"&gt;
                          &lt;ItemsControl ItemsSource="{Binding Bars, RelativeSource={RelativeSource TemplatedParent}}"&gt;
                              &lt;ItemsControl.ItemTemplate&gt;
                                  &lt;DataTemplate&gt;
                                      &lt;TextBlock Text="{Binding Value}"/&gt;
                                  &lt;/DataTemplate&gt;
                              &lt;/ItemsControl.ItemTemplate&gt;
                          &lt;/ItemsControl&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Window.DataContext&gt;
          &lt;sys:String&gt;from DataContext&lt;/sys:String&gt;
      &lt;/Window.DataContext&gt;
      &lt;Grid&gt;
          &lt;this:FooControl&gt;
              &lt;this:Bar Value="directly set"/&gt;
              &lt;this:Bar Value="{Binding Source=from_binding_source}"/&gt;
              &lt;this:Bar Value="{Binding}"/&gt;
              &lt;this:Bar Value="{Binding Text, ElementName=SomeTextBlock}"/&gt;
          &lt;/this:FooControl&gt;
          &lt;TextBlock Text="from TextBox" Name="SomeTextBlock" Visibility="Collapsed"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>output</p>
  
  <pre><code>directly set
  from_binding_source
  "&lt;not set&gt;"
  "&lt;not set&gt;"
  </code></pre>
  
  <p>debug output</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Text; DataItem=null; target element is 'Bar' (HashCode=26568931); target property is 'Value' (type 'String')</p>
  </blockquote>
  
  <p><strong>Any suggestion how to get it to work?</strong></p>
  
  <p>My current workaround is to define the bindings in code but this is a lot more code and looking at the xaml its not obvious which bindings do exists. (see my answer for workaround code)</p>