How do I resolve a WPF constructor error? Type {0} does not support direct content '.'?

The following XAML (below) defines a custom collection in resources and tries 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 message in the constructor “MyCustomCollection type does not support direct content.” I tried setting ContentProperty as recommended on MSDN, but I can’t figure out what to install it. The custom collection object that I use below and very simple.I tried Item, Items and MyCustomItem and can’t think of anything else to try.

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

Any clues as to where I'm going will be mistakenly received. Also hints on how to dig into the WPF object model to see which properties are displayed at runtime, I can understand this too.

Hi

Ryan

+6
wpf designer
source share
1 answer

You must initialize the ContentPropertyAttribute with the name of the property that will represent the contents of your class. In your case, since you are inheriting an ObservableCollection, this will be the Items property. Unfortunately, the Items property is read-only, and this is not valid because the Content property must have a setter. Thus, you must define your own wrapper property around the elements and use it in your attribute - for example:

 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 will be all right. Sorry for doing this in C # when your example is in VB, but I really sucked VB and couldn't get even such a simple thing right ... Anyway, it is equipped to convert it, so - hope this helps.

+5
source share

All Articles