The specified value cannot be assigned to the collection.

Edit

It bothers me for almost a year. I will update the answer and add a reward.

I have a user control that has a 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 - base class for all shapes:

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

Now, look at the screenshots below to see the problem: sometimes (after making changes to xaml elsewhere), the designer goes crazy and stops rendering the entire window, throwing exceptions , and the code compiles and runs without problems . I can close this xaml (designer) and open it again so that the problem goes away. But he always appears.

Question: Is something wrong on my part? Missing attribute? Improper use? How can I fix this problem?


Old question

The ugly situation.

I have 2 UserControl . In manual operation, Graph used. Graph has the Figures property to indicate List<Figure> . There are dozens of numbers that have Figure as a base.

In one UserControl it works fine, in case of other exception exceptions

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

And I do not see the difference, which can cause a problem.


Here is a problematic one screenshot


And here one works

Despite the errors, it compiles and runs the project, but if I need to make changes to the problematic UserControl , it will not show any content (says "Invalid markup"). The graphs are almost the same, all 8 errors are shown for only one UserControl .

What should I do? How to fix such errors? I rule out (completely) any problem with Graph , because it works without a single problem, and it works without problems for another UserControl . Problem with the designer of Visual Studio? Using 2013 Express for Windows Desktop.

+5
source share
3 answers

Indeed, the visual designer does not recognize inheritance from Figure . One solution is to use IList as an 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>())); 

This may seem a little strange (because you are giving up security type). But look at the WPF classes. They all do it (most likely, for good reason). Or WPF even creates collection classes, such as PathFigureCollection , that implement both IList and IList<PathFigure> .

+2
source

close the project, restart VS and open it again. Does he still list errors? visual studio often reports "phantom errors", but they usually go away if you close and restart, etc.

+2
source

If the user control is in the same solution or project, Visual Studio creates it (when it considers it necessary), so it can use the control in the designer.

Sometimes this built-in / cached version goes out of sync with code files, which causes the Xaml parser / syntax to get confused and display these wavy red lines.

I have had success with closing and re-opening all the designers who use the control, but it's pretty annoying to keep doing. In my experience, the most reliable solution is to move the control into a separate solution and project, and set the โ€œcorrectโ€ link to the dll.

+2
source

Source: https://habr.com/ru/post/1205684/