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>
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • More up-to-date discussion at https://stackoverflow.com/questions/2494823/binding-dictionaryt-to-a-wpf-listbox – Ben Feb 26 '18 at 19:14

4 Answers4

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>
Steven Sudit
  • 19,391
  • 1
  • 51
  • 53
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
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>
Alexzander
  • 345
  • 5
  • 8
  • 3
    Namespace declaration: `xmlns:Specialized="clr-namespace:System.Collections.Specialized;assembly=System"` – Pollitzer Feb 28 '16 at 09:41
  • 1
    and `xmlns:System="clr-namespace:System;assembly=mscorlib"` – marbel82 Jun 01 '21 at 13:17
  • This is maybe not such a good idea. In https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.listdictionary?redirectedfrom=MSDN&view=net-6.0 it says: "Recommended for collections that typically include fewer than 10 items." – Kim Homann Sep 02 '22 at 09:42
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>
Laurel
  • 5,965
  • 14
  • 31
  • 57
Bryan Potratz
  • 51
  • 1
  • 2
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.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400