Gus
Gus

Reputation: 10659

How to declare Array of StringDictionary in XAML?

I know I can declare an array of string in XAML like this:

<x:Array Type="{x:Type System:String}">
   <System:String> first </System:String>
   <System:String> second </System:String>
   <System:String> third </System:String>
</x:Array>

How can I declare an array of System.Collections.Specialized.StringDictionary in XAML

<x:Array Type="{x:Type Specialized:StringDictionary}">
    <Specialized:StringDictionary>
        (((what do I put here?)))
    </Specialized:StringDictionary>
</x:Array>

Thanks!

Upvotes: 0

Views: 1716

Answers (2)

Fyodor Soikin
Fyodor Soikin

Reputation: 80805

Nothing. You can't do that in XAML.

There is only one way to populate a StringDictionary: call its Add(string,string) method. XAML does not allow to call methods. Only assign properties, and, as a special case, populate collections that implement ICollection<T>, which StringDictionary doesn't.

Upvotes: 1

Brad Cunningham
Brad Cunningham

Reputation: 6501

No built in way to support it but two options to get you close.

If you are on .NET 4 and are NOT compiling your XAML (highly unlikely) you can do something like this

https://web.archive.org/web/20120118193925/http://blogs.windowsclient.net/rob_relyea/archive/2009/06/01/xaml-using-generic-types-in-xaml-2009.aspx

Again, this is really not a viable solution for most but it is possible.

More than likely you will want to create a custom markup extension to support generic collection initialization. Something like this:

https://web.archive.org/web/20160219214423/http://blogs.msdn.com/b/mikehillberg/archive/2006/10/06/limitedgenericssupportinxaml.aspx

adapt his last solution to include key and value and it should get where you want, sort of

Upvotes: 0

Related Questions