views:

1321

answers:

1

Hi,

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

+2  A: 

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.

Boyan
Looks good, but a question about your setter there. Would not that just continually add to the collection and cause an exception?Also, why IList and not ObservableCollection?Thanks
Ryan ONeill
No, it will add only as much items as you have in the incoming value variable - and those are the values that you define in XAML. And it's IList<T>, because that's the type of the Items property. Remember, the wrapper is for the Items property, not for the whole class.
Boyan
Thanks, that's excellent advice. Much appreciated.
Ryan ONeill
Moved on, I now get 'This item represents a default value and cannot be changed.' in the designer. Perhaps I need to use the setter syntax.
Ryan ONeill
I don't get that error, but I don't know what exactly you're trying to do. Could you specify?
Boyan
There's another issue, though. The Content property allows only a single piece of content - so if you try to put a second MyCustomItem in the XAML, you'll get an error. If that's not irrelevant, you should look for another solution, one that doesn't use the ContentPropertyAttribute.
Boyan