Populating a property of type ObservableCollection in XAML with static resources

1k views Asked by At

I have created a class that contains a property of type ObservableCollection. I am trying to create an instance of the class in XAML and fill this property with members. I keep getting an exception that class T can not be converted to ObservableCollection, but this exception only occurs when I am trying to populate the list with elements that were declared as static resources.

Anybody has an idea why?

The code is as follows:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mseg="clr-namespace:SegApp.Model.DataEntities.Segments;assembly=SegApp.Model.Silverlight">

                         <mseg:Dot xKey="d1"/>
                         <mseg:Dot xKey="d2"/>
                         <mseg:Dot xKey="d3"/>
                         <mseg:Dot xKey="d4"/>

                         <mseg:Segment xKey="seg1">
                             <mseg:Segment.Dots>
                                    <StaticResource ResourceKey="d1"/>
                                    <StaticResource ResourceKey="d2"/>
                                    <StaticResource ResourceKey="d3"/>
                                    <StaticResource ResourceKey="d4"/>
                             </mseg:Segment.Dots>
                         </mseg:Segment>
</ResourceDictionary>

The Class definition is:

public class Segment : Part
{
    public ObservableCollection<Dot> Dots { get; set; }

    public Segment()
    {
        Dots = new ObservableCollection<Dot>();
    }
}

And the exception says: "

Object of type bla.bla.bla.Dot can not be converted to type System.Collections.ObjectModel.ObservableCollection'1[bla.bla.bla.Dot]

"

Any ideas?

2

There are 2 answers

0
Olivier On

As is your code, each element of the collection must be a Dot, not a resource... Each entry of the list in your xaml code must be something like or perhaps try somevalue or {staticResource xxx }

But there is still a problem. The 1st syntax is ok, the second can work if there is a simple content for Dot, but the 3rd can't run : tag means "create an instance of Dot". And a StaticResource means "create an instance of.. and give it a key". So last syntax will certainly not work cause you can replace the instance created by the tag with the instance coming from the resource...

But give it a try. The main problem in your code is than you're trying to feel a collection of Dot with Resource, that can't work and the compiler is not ok.. try using tag to create entry. And then play a bit to see if you can refer the resources somewhere in these tags..

0
Snowbear On

In order to use collections XAML syntax change your property and remove it's setter:

public class Segment : DependencyObject
{
    private readonly ObservableCollection<Dot> _dots = new ObservableCollection<Dot>();
    public ObservableCollection<Dot> Dots
    {
        get { return _dots; }
    }
}