title: Binding a list of elements to a collection-based dependency property in WPF
body: <p>I'm implementing a custom Blend behavior in a WPF application (.Net 4.5). I've added a couple of dependency properties of type FrameworkElement to the behavior class to allow the user of the behavior to bind the elements of the view that they want to control. (This behavior invokes some animation on multiple elements, so I can't just use the AssociatedObject). This works fine and basically looks like this:</p>
  
  <pre><code>public class MyBehavior : Behavior&lt;FrameworkElement&gt;
  {
      public static readonly DependencyProperty Element1Property = DependencyProperty.Register("Element1", typeof (FrameworkElement), typeof (MyBehavior), new UIPropertyMetadata());
  
      public FrameworkElement Element1
      {
          get { return (FrameworkElement) GetValue(Element1Property); }
          set { SetValue(Element1Property, value); }
      }
  
      public static readonly DependencyProperty Element2Property = DependencyProperty.Register("Element2", typeof(FrameworkElement), typeof(MyBehavior), new UIPropertyMetadata());
  
      public FrameworkElement Element2
      {
          get { return (FrameworkElement) GetValue(Element2Property); }
          set { SetValue(Element2Property, value); }
      }
  }
  </code></pre>
  
  <p>Standard dependency property stuff. And I can use that in my view like this:</p>
  
  <pre><code>&lt;Grid x:Name="Container"&gt;
      &lt;i:Interaction:Behaviors&gt;
          &lt;local:MyBehavior
              Element1="{Binding ElementName=FirstElement}"
              Element2="{Binding ElementName=SecondElement}"
          /&gt;
      &lt;/i:Interaction:Behaviors&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>This works great and I can work with the elements in the behavior. But now I have a requirement to bind a list of elements like this. So I don't know ahead of time that there are going to be exactly 2 elements, there could be N elements that I need to work with. So I've added another property to the MyBehavior class like this:</p>
  
  <pre><code>public static readonly DependencyProperty ElementsProperty = DependencyProperty.Register("Elements", typeof(List&lt;FrameworkElement&gt;), typeof(MyBehavior), new UIPropertyMetadata(new List&lt;FrameworkElement&gt;()));
  
  public List&lt;FrameworkElement&gt; Elements
  {
      get { return (List&lt;FrameworkElement&gt;) GetValue(ElementsProperty); }
      set { SetValue(ElementsProperty, value); }
  }
  </code></pre>
  
  <p>(And I've followed the advice <a href="http://msdn.microsoft.com/en-us/library/aa970563(v=vs.110).aspx" rel="nofollow">here</a> to initialize the list in the behavior's constructor.) But I can't figure out how to bind the list of elements to this property from the view's XAML. Basically, I want to do something along these lines:</p>
  
  <pre><code>&lt;Grid x:Name="Container"&gt;
      &lt;i:Interaction:Behaviors&gt;
          &lt;local:MyBehavior&gt;
              &lt;local:MyBehavior.Elements&gt;
                  &lt;Binding ElementName="FirstElement" /&gt;
                  &lt;Binding ElementName="SecondElement" /&gt;
                  &lt;Binding ElementName="ThirdElement" /&gt;
              &lt;/local:MyBehavior.Elements&gt;
          &lt;/local:MyBehavior&gt;
      &lt;/i:Interaction:Behaviors&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>But of course this doesn't actually work. I've tried MultiBinding here, but that doesn't work either. Any idea what the XAML syntax would be for doing this, or is it even possible? If it's not possible, any ideas for other approaches to achieve this effect? Thanks!</p>