如何解决 WPF 设计器错误“类型 {0} 不支持直接内容".? [英] How do I resolve the WPF Designer error 'The type {0} does not support direct content'.'?

查看:28
本文介绍了如何解决 WPF 设计器错误“类型 {0} 不支持直接内容".?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 XAML(如下)定义了资源中的自定义集合并尝试使用自定义对象填充它;

问题是我在设计器中收到'MyCustomCollection' 类型不支持直接内容'的错误.我已经尝试按照 MSDN 中的建议设置 ContentProperty,但无法弄清楚将其设置为什么.我使用的自定义集合对象在下面,非常简单.我已经尝试过 Item、Items 和 MyCustomItem,但想不出还能尝试什么.

_公共类 MyCustomCollection继承 ObservableCollection(Of MyCustomItem)结束类

任何有关我哪里出错的线索将不胜感激.还提示了如何深入 WPF 对象模型以查看在运行时公开的属性,我也可以通过这种方式弄清楚.

问候

瑞恩

解决方案

您必须使用将表示您的类内容的属性的名称来初始化 ContentPropertyAttribute.在您的情况下,因为您从 ObservableCollection 继承,这将是 Items 属性.不幸的是,Items 属性是只读的,这是不允许的,因为 Content 属性必须有一个 setter.因此,您必须围绕 Items 定义一个自定义包装器属性并在您的属性中使用它 - 像这样:

公共类 MyCustomItem{ }[ContentProperty("MyItems")]公共类 MyCustomCollection : ObservableCollection{公共 IList我的物品{得到{返回项目;}放{foreach(值中的 MyCustomItem 项){Items.Add(item);}}}}

你应该没事的.抱歉,当您的示例在 VB 中时,在 C# 中这样做,但我真的很讨厌 VB,甚至连这么简单的事情都做对了……无论如何,转换它很容易,所以 - 希望有所帮助.

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

解决方案

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.

这篇关于如何解决 WPF 设计器错误“类型 {0} 不支持直接内容".?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆