Create a Dictionary in xaml?
Asked Answered
S

4

23

Pseudo example:

<Window>
  <Window.Tag>
    <x:Dictionary KeyType="{x:Type sys:String}" ValueType="{x:Type sys:Int32}">
        <sys:DictionaryEntry Entry="{sys:DictionaryEntry Key0, 000}"/>
        <sys:DictionaryEntry Key="key1" Value="111"/>
        <sys:DictionaryEntry>
          <sys:DictionaryEntry.Key>
            <sys:String>Key2<sys:String>
          </sys:DictionaryEntry.Key>          
          <sys:DictionaryEntry.Value>
            <sys:Int32>222</sys:Int32>            
          </sys:DictionaryEntry.Value>
        </sys:DictionaryEntry>
    </x:Dictionary />    
  </Window.Tag>
</Window>
Suzetta answered 23/2, 2010 at 15:13 Comment(1)
More up-to-date discussion at #2495323Mychael
P
34

You can't use the Dictionary<TKey, TValue> class directly in XAML, because there's no way to specify the generic type arguments (it will be possible in the next version of XAML, but it won't be supported in VS2010 WPF designer... at least not in the initial release).

However, you can declare a non-generic class that inherits from Dictionary<TKey, TValue>, and use it in XAML.

C#

public class MyDictionary : Dictionary<string, int> { }

XAML

<Window>
  <Window.Tag>
    <local:MyDictionary>
        <sys:Int32 x:Key="key0">0</sys:Int32>
        <sys:Int32 x:Key="key1">111</sys:Int32>
        <sys:Int32 x:Key="key2">222</sys:Int32>
    </local:MyDictionary />    
  </Window.Tag>
</Window>
Proceleusmatic answered 23/2, 2010 at 16:14 Comment(5)
What is meant by it will be possible in the next version of XAML do you have any clue when they plan to implement it?Suzetta
@Shimmy : actually, it is already implemented in XAML 2009. Unfortunately VS2010 doesn't support XAML 2009 yet :(Proceleusmatic
For details, see this video : channel9.msdn.com/pdc2008/TL36 (XAML 2009 new features are presented starting at 7'20)Proceleusmatic
what do you mean 7'20, is this when I will be 70yo :{Suzetta
I mean at the position "7 minutes and 20 seconds" in the videoProceleusmatic
C
8

If the keys and values are strings, you can use ListDictionary or HybridDictionary.

For example:

<Specialized:ListDictionary x:Key="MasterSlidesFileNames">
    <System:String x:Key="long">Ya long yes ni</System:String>
    <System:String x:Key="Sun">Waterfall</System:String>
    <System:String x:Key="lorem ipsum">hello wOrld</System:String>
</Specialized:ListDictionary>
Cerement answered 24/6, 2013 at 14:49 Comment(3)
Namespace declaration: xmlns:Specialized="clr-namespace:System.Collections.Specialized;assembly=System"Soosoochow
and xmlns:System="clr-namespace:System;assembly=mscorlib"Needy
This is maybe not such a good idea. In learn.microsoft.com/en-us/dotnet/api/… it says: "Recommended for collections that typically include fewer than 10 items."Rhinal
I
5

In a related question i gave an answer which shows how one could create a generic dictionary in XAML without the XAML 2009 features using a custom Markup Extension instead.

Insuperable answered 26/2, 2012 at 2:44 Comment(1)
I checked it. Yes it's great!Suzetta
P
5

Try something like this:

use this namespace: xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"

<ComboBox.ItemsSource>
    <collections:ArrayList>
        <collections:DictionaryEntry Key="0" Value="Standby"/>
        <collections:DictionaryEntry Key="1" Value="Maintenance"/>
        <collections:DictionaryEntry Key="2" Value="Available"/>
        <collections:DictionaryEntry Key="3" Value="Deselected"/>
        <collections:DictionaryEntry Key="4" Value="Input Error"/>
    </collections:ArrayList>
</ComboBox.ItemsSource>
Plantar answered 30/6, 2016 at 21:22 Comment(2)
New answer to an old post, but exactly what I needed.Amphibolous
Does this avoid duplicated keys? My guess is "no" :)Cerebellum

© 2022 - 2024 — McMap. All rights reserved.