I'm trying to create a custom set of classes that can be added to a WPF control through XAML.
The problem I'm having is adding items to the collection. Here's what I have so far.
public class MyControl : Control
{
static MyControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
}
public static DependencyProperty MyCollectionProperty = DependencyProperty.Register("MyCollection", typeof(MyCollection), typeof(MyControl));
public MyCollection MyCollection
{
get { return (MyCollection)GetValue(MyCollectionProperty); }
set { SetValue(MyCollectionProperty, value); }
}
}
public class MyCollectionBase : DependencyObject
{
// This class is needed for some other things...
}
[ContentProperty("Items")]
public class MyCollection : MyCollectionBase
{
public ItemCollection Items { get; set; }
}
public class MyItem : DependencyObject { ... }
And the XAML.
<l:MyControl>
<l:MyControl.MyCollection>
<l:MyCollection>
<l:MyItem />
</l:MyCollection>
</l:MyControl.MyCollection>
</l:MyControl>
The exception is:
System.Windows.Markup.XamlParseException occurred
Message="'MyItem' object cannot be added to 'MyCollection'. Object of type 'CollectionTest.MyItem' cannot be converted to type 'System.Windows.Controls.ItemCollection'.
Does any one know how I can fix this? Thanks