Search code examples
c#wpfxaml

The specified value cannot be assigned to the collection


Edit

This bugs me for an almost year. I'll update the answer and add bounty.

I've custom control, which has dependency property

public class Graph : Control
{
    public List<Figure> Figures
    {
        get { return (List<Figure>)GetValue(FiguresProperty); }
        set { SetValue(FiguresProperty, value); }
    }
    public static readonly DependencyProperty FiguresProperty =
        DependencyProperty.Register("Figures", typeof(List<Figure>), typeof(Graph),
        new PropertyMetadata((d, e) => ((Graph)d).InvalidateVisual()));
    ...
}

Figure is the base class for all figures:

public abstract class Figure { ... }

public class LineFigure : Figure { ... }
public class XGridFigure : Figure { ... }
public class YGridFigure : Figure { ... }
...

Now look at screenshots below to see the problem: sometimes (after doing a change to xaml in other place) designer goes crazy about it and stop rendering the whole window, throwing exceptions, while code compiles and runs without problem. I can close this xaml (designer) and open it again to make problem go away. But it always reappears.

Question: is there something wrong on my side? Missing attribute? Wrong usage? How can I fix that problem?


Old question

Ugly situation.

I have 2 UserControl. In both hand-made control Graph is used. Graph has property Figures to specify List<Figure>. There are dozens of figures which have Figure as base.

In one UserControl it works fine, in other throws exception

The specified value cannot be assigned to the collection. The following type was expected: "Figure".

And I fail to see a difference what could cause a problem.


Here is problematic one screenshot


And here is working one

Despite of errors project compiles and runs, but if I need to do modification to problematic UserControl, then it's not showing any content (says "Invalid Markup"). Graphs are nearly the same, all 8 errors are shown for to just one UserControl.

What should I do? How to troubleshoot such errors? I exclude (completely) any problem with Graph because it runs without a single problem AND it works without problem for another UserControl. Visual Studio designer problem? Using 2013 Express for Windows Desktop.


Solution

  • Indeed the visual designer does not recognize the inheritance from Figure. One solution is to use IList as the Interface type:

        public IList Figures
        {
            get
            {
                return (IList)GetValue (FiguresProperty);
            }
            set
            {
                SetValue (FiguresProperty, value);
            }
        }
    
        public static readonly DependencyProperty FiguresProperty =
            DependencyProperty.Register ("Figures", typeof (IList), typeof (Graph), new PropertyMetadata (new List<object>()));
    

    That might look like a bit strange (because you give up type safetyness). But have a closer look at the WPF classes. They all do it that way (most likely for good reasons). Or WPF even creates collection classes like PathFigureCollection that implement both IList and IList<PathFigure>.