Rya*_*ill 7 wpf designer contentproperty
以下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>
Run Code Online (Sandbox Code Playgroud)
问题是我在'类型'的设计者中遇到错误.MyCustomCollection'不支持直接内容'.我已经尝试在MSDN中建议设置ContentProperty,但无法弄清楚要将其设置为什么.我使用的自定义集合对象如下,非常简单.我已经尝试了Item,Items和MyCustomItem,并且无法想到还有什么可以尝试.
<ContentProperty("WhatGoesHere?")> _
Public Class MyCustomCollection
Inherits ObservableCollection(Of MyCustomItem)
End Class
Run Code Online (Sandbox Code Playgroud)
我将非常感激地收到关于我出错的任何线索.还提示如何深入了解WPF对象模型以查看在运行时公开的属性,我也可以通过这种方式来理解它.
问候
瑞安
您必须使用将代表您的类的内容的属性的名称初始化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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你应该没事.很抱歉,当你的例子在VB中时,在C#中做到这一点,但我真的很厌烦VB,甚至无法做到这么简单的事情......无论如何,转换它很容易,所以 - 希望有所帮助.
| 归档时间: |
|
| 查看次数: |
7861 次 |
| 最近记录: |