locked
Dependency Property Issue

    Question

  • I am defining dependency property like this in control:

    public ItemCollection Items
    {
        get { return ( ItemCollection ) GetValue ( ItemsProperty ); }
        set { SetValue ( ItemsProperty, value ); }
    }
    
    // Using a DependencyProperty as the backing store for Items.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ItemsProperty = 
    DependencyProperty.Register ( "Items", typeof ( ItemCollection ), typeof ( MyUserControl ), new PropertyMetadata ( null ) );

    But it is not working and give error that unable to create ItemCollection.

    I am using it like this:

    <MyUserControl>
         <MyUserControl.Items>
              <Button></Button>
         </MyUserControl.Items>
    </MyUserControl>

    I want to achieve functionality similar to <ItemsControl.Items>.

    Also, I would like to know how to bind <ItemsControl.Items> property

    Any idea how to do it?

    Tuesday, July 01, 2014 2:06 PM

Answers

  • You get the error because the ItemCollection class doesn't have any public parameterless constructor so you cannot create an instance of it nor inherit from it as the class is sealed. 

    You should use another built-in collection type such as a List<T> for example.

    • Marked as answer by Talha.Ahmed Tuesday, July 01, 2014 10:36 PM
    Tuesday, July 01, 2014 2:49 PM