如何解决WPF设计错误“的类型{0}不支持直接的内容”。“?
-
21-08-2019 - |
题
下面的XAML(下面)定义资源的自定义集合并尝试与自定义对象来填充它;
<UserControl x:Class="ImageListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300"
xmlns:local="clr-namespace:MyControls" >
<UserControl.Resources>
<local:MyCustomCollection x:Key="MyKey">
<local:MyCustomItem>
</local:MyCustomItem>
</local:MyCustomCollection>
</UserControl.Resources>
</UserControl>
的问题是,我得到了“类型‘MyCustomCollection的设计者是一个错误’不支持直接的内容”。我试过设置ContentProperty诚如在MSDN,但无法弄清楚如何将它设置为。我使用自定义集合对象是以下,是非常简单的。我已经试过项目,项目和MyCustomItem和想不出什么尝试。
<ContentProperty("WhatGoesHere?")> _
Public Class MyCustomCollection
Inherits ObservableCollection(Of MyCustomItem)
End Class
任何线索,我要去的地方错将受到欢迎。也暗示了如何向下挖掘到WPF对象模型,看看属性在运行时暴露出来,我也许可以这样计算出来了。
此致
赖安
解决方案
您有那将代表你的类的内容属性的名称来初始化ContentPropertyAttribute。在你的情况,因为你的ObservableCollection继承,这将是Items属性。不幸的是,项目属性为只读,并且是不允许的,因为内容属性必须有一个setter。所以,你必须定义围绕项目自定义包装财产和使用,在你的属性 - 像这样:
public class MyCustomItem
{ }
[ContentProperty("MyItems")]
public class MyCustomCollection : ObservableCollection<MyCustomItem>
{
public IList<MyCustomItem> MyItems
{
get { return Items; }
set
{
foreach (MyCustomItem item in value)
{
Items.Add(item);
}
}
}
}
和你应该没事。对不起,我这样做,在C#中,当你的例子是VB,但我真的吸在VB,不能得到连这么简单的正确的事情......不管怎么说,这是一个管理单元,以将它转换,这样 - 希望帮助
不隶属于 StackOverflow