wpf 指定的值不能分配给集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26606280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The specified value cannot be assigned to the collection
提问by Sinatr
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()));
...
}
Figureis the base class for all figures:
Figure是所有图形的基类:
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.
现在看看下面的截图看看问题所在:有时(在其他地方对 xaml 进行更改后)设计人员会为此疯狂并停止渲染整个窗口,抛出异常,而代码编译和运行没有问题。我可以关闭这个 xaml(设计器)并再次打开它以使问题消失。但它总是会再次出现。
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 Graphis used. Graphhas property Figuresto specify List<Figure>. There are dozens of figures which have Figureas base.
我有 2 UserControl。两者均采用手工制作控制Graph。Graph有Figures要指定的属性List<Figure>。有数十个数字Figure作为基础。
In one UserControlit works fine, in other throws exception
在一个UserControl它工作正常,在其他抛出异常
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 8errors are shown for to just one UserControl.
尽管项目编译和运行出错,但如果我需要对有问题的 进行修改UserControl,那么它不会显示任何内容(说“无效标记”)。图表几乎相同,所有8错误仅显示一个UserControl。
What should I do? How to troubleshoot such errors? I exclude (completely) any problem with Graphbecause it runs without a single problem AND it works without problem for another UserControl. Visual Studio designer problem? Using 2013 Express for Windows Desktop.
我该怎么办?如何解决此类错误?我排除(完全)任何问题,Graph因为它运行时没有任何问题,并且对于另一个UserControl. Visual Studio 设计器问题?使用 2013 Express for Windows 桌面。
采纳答案by gomi42
Indeed the visual designer does not recognize the inheritance from Figure. One solution is to use IList as the Interface type:
事实上,视觉设计师不承认从Figure. 一种解决方案是使用 IList 作为接口类型:
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 PathFigureCollectionthat implement both IListand IList<PathFigure>.
这可能看起来有点奇怪(因为您放弃了类型安全性)。但是仔细看看 WPF 类。他们都这样做(很可能有充分的理由)。或者 WPF 甚至创建了这样的集合类PathFigureCollection,同时实现IList和IList<PathFigure>.
回答by Theodosius Von Richthofen
close the project, restart VS and reopen it. does it still list the errors? visual studio often seems to report "phantom errors", but they usually go away if you close and restart etc.
关闭项目,重新启动VS并重新打开它。它仍然列出错误吗?Visual Studio 似乎经常报告“幻像错误”,但如果您关闭并重新启动等,它们通常会消失。
回答by Erno
If the custom control is in the same solution or project, Visual Studio builds it (when it considers it necessary) so it can use the control in the designer.
如果自定义控件在同一个解决方案或项目中,Visual Studio 会构建它(当它认为有必要时),以便它可以在设计器中使用该控件。
Sometimes this built/cached version gets out of sync with the code files which causes the Xaml parser/syntax checker to get confused and display those wavy red lines.
有时,此构建/缓存版本与代码文件不同步,这会导致 Xaml 解析器/语法检查器混淆并显示那些波浪状红线。
I have had success with closing and reopening all designers that use the control but that is pretty annoying to keep on doing. In my experience the most reliable solution is to move the control into a separate solution and project and set a 'proper' reference to the dll.
我已经成功关闭并重新打开所有使用该控件的设计器,但继续这样做很烦人。根据我的经验,最可靠的解决方案是将控件移动到单独的解决方案和项目中,并设置对 dll 的“正确”引用。

