0
<Window.Resource>
    <ResourceDictionary>
        <local:SomeResourceWithObsCollection x:Key="MyItemWithCollection">
            <local:SomeClass.Instance /> <!-- THIS DOES NOT WORK -->
        </local:SomeResourceWithObsCollection>
    </ResourceDictionary>
</Window.Resources>

I don't know how to get that line to work... I've tried doing <x:Static SomeClass.Instance />, but that also isn't allowed.

[ContentProperty("TheItems")]
public class SomeResourceWithObsCollection
{
    public class SomeResourceWithObsCollection()
    {
        TheItems = new ObservableCollection<IMyInterface>();
    }

    public ObservableCollection<IMyInterface> TheItems { get; set; }
}

public class SomeClass : IMyInterface
{
    private static SomeClass _instance = new SomeClass();

    private SomeClass() { }

    public SomeClass Instance { get { return _instance; } }
}
michael
  • 14,844
  • 28
  • 89
  • 177
  • What are you *trying* to get it to do? Can you post details of the type `SomeResourceWithObsCollection`? – Steve Greatrex Oct 06 '11 at 21:28
  • I posted what is actually relavent. Basically I have singleton instances. If I remove the fact that these items are singletons all works fine. But, I want to be able to use singleton instances in XAML basically. – michael Oct 06 '11 at 21:34

2 Answers2

2

You can't do what you're asking to do in XAML as of right now. Perhaps future versions of XAML will account for this. You have to do it in the code behind, here is an example:

Adding a static object to a resource dictionary

Community
  • 1
  • 1
myermian
  • 31,823
  • 24
  • 123
  • 215
1

The closest I can suggest is a combination of the CompositeCollection and using ListBoxItems (or some other equivalent) to wrap your static content (as I believe you can only pull static content into XAML using the {x:Static} markup extension)

This can be used in XAML as below:

<ListBox>
    <ListBox.ItemsSource>
        <CompositeCollection>
            <ListBoxItem Content="{x:Static local:Example.One}" />
            <ListBoxItem Content="{x:Static local:Example.Two}" />
        </CompositeCollection>
    </ListBox.ItemsSource>
</ListBox>
Steve Greatrex
  • 15,789
  • 5
  • 59
  • 73
  • I don't want to get a ListBoxItem. I want an instance of my object to get loaded into my obs-collection as a resource. – michael Oct 07 '11 at 01:21