title: Adding a UIElementCollection DependencyProperty in Silverlight
body: <p>I want to add a dependency property to a <code>UserControl</code> that can contain a collection of <code>UIElement</code> objects. You might suggest that I should derive my control from <code>Panel</code> and use the <code>Children</code> property for that, but it is not a suitable solution in my case.</p>
  
  <p>I have modified my <code>UserControl</code> like this:</p>
  
  <pre><code>public partial class SilverlightControl1 : UserControl {
  
    public static readonly DependencyProperty ControlsProperty
      = DependencyProperty.Register(
        "Controls",
        typeof(UIElementCollection),
        typeof(SilverlightControl1),
        null
      );
  
    public UIElementCollection Controls {
      get {
        return (UIElementCollection) GetValue(ControlsProperty);
      }
      set {
        SetValue(ControlsProperty, value);
      }
    }
  
  }
  </code></pre>
  
  <p>and I'm using it like this:</p>
  
  <pre><code>&lt;local:SilverlightControl1&gt;
    &lt;local:SilverlightControl1.Controls&gt;
      &lt;Button Content="A"/&gt;
      &lt;Button Content="B"/&gt;
    &lt;/local:SilverlightControl1.Controls&gt;
  &lt;/local:SilverlightControl1&gt;
  </code></pre>
  
  <p>Unfortunately I get the following error when I run the application:</p>
  
  <pre><code>Object of type 'System.Windows.Controls.Button' cannot be converted to type
  'System.Windows.Controls.UIElementCollection'.
  </code></pre>
  
  <p>In the <a href="http://msdn.microsoft.com/en-us/library/cc189036(VS.95).aspx#declaringproperties" rel="nofollow noreferrer">Setting a Property by Using a Collection Syntax</a> section it is explicitly stated that:</p>
  
  <blockquote>
    <p>[...] you cannot specify [UIElementCollection] explicitly in XAML, because UIElementCollection is not a constructible class.</p>
  </blockquote>
  
  <p>What can I do to solve my problem? Is the solution simply to use another collection class instead of <code>UIElementCollection</code>? If yes, what is the recommended collection class to use?</p>