WPF Combobox BindingBinding to a dictionary can be tricky.

It sounds simple but it never works on the first try. Usually when you first run you application you see that instead of the beautiful template you created for the items, you get something that looks like a pair of key and value. your binding works fine, you just did not think about what are you binding to. every item in a dictionary is a pair (Key,Value) and that is exactly what you get as the binded item. You have two ways of handle this, You can either change the markup of the Binding expression to include the reference to the Value :

                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Value.Text}"></TextBlock>
                        <TextBlock> = </TextBlock>
                        <TextBlock Text="{Binding Value.Value}"></TextBlock>

or you can change the binding expression of the control to refer to the Values property of the dictionary:

<ComboBox Height="23" Margin="0,14,9,0" Name="comboBox1"
                  VerticalAlignment="Top" SelectedValuePath="Key"
                  ItemsSource="{Binding Items.Values}"
                  HorizontalAlignment="Right" Width="120">

Now you have your control binded to a Dictionary, Hurray!

Subscribe to our feed and get the complete code sample for this article



Tags :

9 Responses to “Binding a WPF Control To a Dictionary”

  1. Martin Cook

    Said on June 19, 2008 :

    Nice one, thanks! The top one worked for me, but not the second one. Perhaps because it should be ItemsSource=”{Binding Items.Value}” instead of ItemsSource=”{Binding Items.Values}” but I’d changed my code to use the first method before spotting that might be the case.

    Thanks again!

  2. Martin Cook

    Said on June 19, 2008 :

    Upon further experimentation this is how I got the first example to work: –


    Dictionary indexedObjects = new Dictionary();
    vehicleList.DataContext = indexedObjects.Values;


  3. Martin Cook

    Said on June 19, 2008 :

    Heh, cant post XAML then.

    Ah well.

  4. Mario

    Said on June 18, 2009 :

    other nice & simple dictionary binding in an MVVM Pattern:

    u define a Dictionary _Translation
    and fill this
    Translation = TranslateXml.GetTranslatedDictionaryFromXML();
    and then bind in your view like this:
    MenuItem Header=”{Binding Translation[MenuItemEdit]}”

    greets mario

  5. Joseph Melettukunnel

    Said on July 8, 2009 :

    Doesn’t seem to work. Here is my solution:

    Also check out http://stackoverflow.com/questions/800130/two-way-data-binding-with-a-dictionary-in-wpf/800217 if you’re looking for a TwoWay-Dictionary Solution.


  6. florian.reischer

    Said on February 14, 2010 :

    Hello, this example seems nice, i also got it so far on my own, but i would like to do a binding like this:

    with a Dictionary of i can use the bool on a textblock but on the IsChecked it does not work, when extending the tree view, the gui freezes… :-( Any advice?

  7. Lawrence Robinson

    Said on April 16, 2010 :

    I tried both work-arounds with zero success. However, with the first one, I noted the dropdownlist’s DataContext receives the [dictionary].Values list but the dropdownlist is neither populated nor does it appear that binding ever occurs. Any insights would be greatly appreciated. Here’s my Xaml and code-behind:

    cboSchedules.DataContext = schedules.Values;

  8. Peter

    Said on September 16, 2011 :

    Thanks a million – it was silently binding successfully but not showing any items until I followed your advice to set this up in XAML:

1 Trackback(s)

  1. Sep 6, 2009: WPF: 90+ Miejsc które warto zna? « Dawid Po?li?ski

Post a Comment