title: How to bind ItemsControl in DataGrid behavior
body: <p>I have this custom vertical scrollbar that is defined in UserControl.Resources, it has a ItemsControl in it called 'ItemsSelected'.</p>
  
  <p>What I would to do is bind it to the DependencyProperty ItemsControlObject in behavior DataGridSelectionChanged. The example binding does not work but shows what I'd like to achieve. What am I missing to bind ItemsSelected?</p>
  
  <p>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=ItemsSelected'. 
  BindingExpression:(no path); 
  DataItem=null; 
  target element is 'DataGridSelectionChanged' (HashCode=43407976); 
  target property is 'ItemsControlObject' (type 'ItemsControl')</p>
  
  <pre><code>&lt;UserControl&gt;
      &lt;UserControl.Resources&gt;
          &lt;ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}"&gt;
              &lt;Grid&gt;
                  ...
                  &lt;!-- BEGIN --&gt;
                  &lt;ItemsControl Name="ItemsSelected" VerticalAlignment="Stretch"
                                ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.MarkerCollection}"&gt;
                      &lt;ItemsControl.ItemTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;Rectangle Fill="Gray" Width="18" Height="4"/&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/ItemsControl.ItemTemplate&gt;
                      &lt;ItemsControl.ItemContainerStyle&gt;
                          &lt;Style TargetType="ContentPresenter"&gt;
                              &lt;Setter Property="Canvas.Top" Value="{Binding}" /&gt;
                          &lt;/Style&gt;
                      &lt;/ItemsControl.ItemContainerStyle&gt;
                      &lt;ItemsControl.ItemsPanel&gt;
                          &lt;ItemsPanelTemplate&gt;
                              &lt;Canvas/&gt;
                          &lt;/ItemsPanelTemplate&gt;
                      &lt;/ItemsControl.ItemsPanel&gt;
                  &lt;/ItemsControl&gt;
                  &lt;!-- END --&gt;
              &lt;/Grid&gt;
          &lt;/ControlTemplate&gt;
  
          &lt;Style TargetType="{x:Type DataGrid}" &gt;
              &lt;Style.Resources&gt;
                  &lt;Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}"&gt;
                      &lt;Setter Property="SnapsToDevicePixels" Value="True" /&gt;
                      &lt;Setter Property="OverridesDefaultStyle" Value="true" /&gt;
                      &lt;Style.Triggers&gt;
                          &lt;Trigger Property="Orientation" Value="Horizontal"&gt;
                              &lt;Setter Property="Width" Value="Auto" /&gt;
                              &lt;Setter Property="Height" Value="18" /&gt;
                              &lt;Setter Property="Template" Value="{StaticResource HorizontalScrollBar}" /&gt;
                          &lt;/Trigger&gt;
                          &lt;Trigger Property="Orientation" Value="Vertical"&gt;
                              &lt;Setter Property="Width" Value="18" /&gt;
                              &lt;Setter Property="Height" Value="Auto" /&gt;
                              &lt;Setter Property="Template" Value="{StaticResource VerticalScrollBar}" /&gt;
                          &lt;/Trigger&gt;
                      &lt;/Style.Triggers&gt;
                  &lt;/Style&gt;
              &lt;/Style.Resources&gt;
          &lt;/Style&gt;
      &lt;/UserControl.Resources&gt;
  
      &lt;Grid Name="gridUsers" Background="Transparent"&gt;
          &lt;DockPanel&gt;
              &lt;DataGrid Name="GenericDataGrid"&gt;
                  &lt;i:Interaction.Behaviors&gt;
                      &lt;helpers:DataGridSelectionChanged ItemsControlObject="{Binding ElementName=ItemsSelected}" /&gt;
                  &lt;/i:Interaction.Behaviors&gt;
                  &lt;DataGrid.Columns&gt;
                      ...
                  &lt;/DataGrid.Columns&gt;
              &lt;/DataGrid&gt;
          &lt;/DockPanel&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>[EDIT]</p>
  
  <pre><code>public static class ScrollBarMarkers
  {
      public static readonly DependencyProperty MarkersSelectedCollectionProperty =
          DependencyProperty.RegisterAttached("MarkersSelectedCollection", typeof(ObservableCollection&lt;double&gt;), typeof(ScrollBarMarkers), new PropertyMetadata(null));
  
      public static ObservableCollection&lt;double&gt; GetMarkersSelectedCollection(DependencyObject obj)
      {
          return (ObservableCollection&lt;double&gt;)obj.GetValue(MarkersSelectedCollectionProperty);
      }
  
      public static void SetMarkersSelectedCollection(ItemsControl obj, ObservableCollection&lt;double&gt; value)
      {
          obj.SetValue(MarkersSelectedCollectionProperty, value);
      }
  }
  </code></pre>