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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With