I have a custom control Workspace
that inherits from Control
and within it is a DependencyProperty
that I need to contain a user-specified IEnumerable<IFoo>
(I have also tried making it an non-generic IEnumerable
).
Public Shared ReadOnly FoosProperty As DependencyProperty = DependencyProperty.Register("Foos", GetType(IEnumerable(Of IFoo)), GetType(Workspace), New FrameworkPropertyMetadata())
Public Property Foos() As IEnumerable(Of IFoo)
Get
Return CType(Me.GetValue(FoosProperty), IEnumerable(Of IFoo))
End Get
Set(ByVal value As IEnumerable(Of IFoo))
Me.SetValue(FoosProperty, CType(value, IEnumerable(Of IFoo)))
End Set
End Property
Everything works perfectly when I create and set an array of IFoo
in code but when I try to add them in XAML I get errors. If I add a single IFoo
I get the error
- "'FooItem' is not a valid value for property 'Foos'."
at run time. If I try to add multiple IFoo
items I get three errors at compile time
- The object 'Workspace' already has a child and cannot add 'FooItem'. 'Workspace' can accept only one child.
- Property 'Foos' does not support values of type 'FooItem'.
- The property 'Foos' is set more than once.
I read the errors to mean that WPF isn't converting the xaml to an array of items like it normally would. Here is how I'm trying to add the items in XAML
<Workspace>
<Workspace.Foos>
<FooItem />
<FooItem />
</Workspace.Foos>
</Workspace>
I have created similar DependencyProperties in the past and never had a problem so I'm guessing I'm missing something simple.
Thanks for any help!