以下 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’类型不支持直接内容”。我已尝试按照 MSDN 中的建议设置 ContentProperty,但不知道将其设置为什么。我使用的自定义集合对象如下,非常简单。我已经尝试过 Item、Items 和 MyCustomItem,但想不出还有什么可以尝试。
<ContentProperty("WhatGoesHere?")> _
Public Class MyCustomCollection
Inherits ObservableCollection(Of MyCustomItem)
End Class
任何有关我出错的地方的线索将不胜感激。还提示如何深入研究 WPF 对象模型以查看在运行时公开哪些属性,我也许也能通过这种方式弄清楚。
Regards
Ryan
您必须使用代表类内容的属性名称来初始化 ContentPropertyAttribute。在您的情况下,因为您继承自 ObservableCollection,所以这将是 Items 属性。不幸的是,Items 属性是只读的,这是不允许的,因为 Content 属性必须有一个 setter。因此,您必须围绕 Items 定义一个自定义包装属性,并在您的属性中使用它 - 如下所示:
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);
}
}
}
}
你应该没事。抱歉,当你的示例是在 VB 中时,我在 C# 中这样做,但我真的很糟糕 VB,甚至连这么简单的事情都无法正确完成...无论如何,转换它很简单,所以 - 希望有所帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)