作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下 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>
<ContentProperty("WhatGoesHere?")> _
Public Class MyCustomCollection
Inherits ObservableCollection(Of MyCustomItem)
End Class
最佳答案
您必须使用将代表您的类的内容的属性的名称来初始化 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);
}
}
}
}
关于wpf - 如何解决 WPF 设计器错误 'The type {0} does not support direct content' .'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/356663/
我是一名优秀的程序员,十分优秀!