如何解决 WPF 设计器错误“类型 {0} 不支持直接内容”。?

发布于 2024-07-09 17:54:05 字数 1022 浏览 7 评论 0原文

以下 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 对象模型以查看在运行时公开哪些属性,我也许也能通过这种方式弄清楚。

问候

瑞安

The following XAML (below) defines a custom collection in resources and attempts to populate it with a custom object;

<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>

The problem is that I get an error in the designer of 'The type 'MyCustomCollection' does not support direct content'. I've tried setting ContentProperty as advised in MSDN but can't figure out what to set it to. The custom collection object which I use is below and is very simple. I've tried Item, Items and MyCustomItem and can't think of what else to try.

<ContentProperty("WhatGoesHere?")> _
Public Class MyCustomCollection
    Inherits ObservableCollection(Of MyCustomItem)
End Class

Any clues as to where I am going wrong would be gratefully received. Also hints on how to dig down into the WPF object model to see what properties are exposed at run-time, I might be able to figure it out that way too.

Regards

Ryan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨离汐 2024-07-16 17:54:05

您必须使用代表类内容的属性名称来初始化 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,甚至连这么简单的事情都无法正确完成...无论如何,转换它很简单,所以 - 希望有所帮助。

You have to initialize the ContentPropertyAttribute with the name of the property that's going to represent the content of your class. In your case, because you inherit from ObservableCollection, that would be the Items property. Unfortunately, the Items property is read-only and that's not allowed, because the Content property must have a setter. So you have to define a custom wrapper property around Items and use that in your attribute - like so:

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);
            }
       }
    }
}

And you should be all right. Sorry for doing that in C# when your example is in VB, but I really suck at VB and couldn't get even such a simple thing right... Anyway, it's a snap to convert it, so - hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文