Note that there are some explanatory texts on larger screens.

plurals
  1. POIEnumerable DependencyProperty throws errors when set in XAML
    text
    copied!<p>I have a custom control <code>Workspace</code> that inherits from <code>Control</code> and within it is a <code>DependencyProperty</code> that I need to contain a user-specified <code>IEnumerable&lt;IFoo&gt;</code> (I have also tried making it an non-generic <code>IEnumerable</code>). </p> <pre><code>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 </code></pre> <p>Everything works perfectly when I create and set an array of <code>IFoo</code> in code but when I try to add them in XAML I get errors. If I add a single <code>IFoo</code> I get the error</p> <ol> <li>"'FooItem' is not a valid value for property 'Foos'."</li> </ol> <p>at run time. If I try to add multiple <code>IFoo</code> items I get three errors at compile time</p> <ol> <li>The object 'Workspace' already has a child and cannot add 'FooItem'. 'Workspace' can accept only one child.</li> <li>Property 'Foos' does not support values of type 'FooItem'.</li> <li>The property 'Foos' is set more than once.</li> </ol> <p>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</p> <pre><code>&lt;Workspace&gt; &lt;Workspace.Foos&gt; &lt;FooItem /&gt; &lt;FooItem /&gt; &lt;/Workspace.Foos&gt; &lt;/Workspace&gt; </code></pre> <p>I have created similar DependencyProperties in the past and never had a problem so I'm guessing I'm missing something simple.</p> <p>Thanks for any help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload