Directory Freebies VS CheatSheet Forum

RSS

Email

Translate

Home About Archive Privacy Contact Advertise Write for Dev102
Posted by Amit on Mar 7th, 2008 | Filed under .Net, C#, WPF |

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 :

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

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

Enjoy

Amit.

Tags: , , , , , ,

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


  1. Martin Cook Said on Jun 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!
    Martin

  2. Martin Cook Said on Jun 19, 2008 :

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

    XAML:

    C#:
    Dictionary indexedObjects = new Dictionary();
    vehicleList.DataContext = indexedObjects.Values;

    Cheers,
    Martin

  3. Martin Cook Said on Jun 19, 2008 :

    Heh, cant post XAML then.

    Ah well.

  4. Mario Said on Jun 18, 2009 :

    other nice & simple dictionary binding in an MVVM Pattern:

    ViewModelProperty:
    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 Jul 8, 2009 :

    Doesn’t seem to work. Here is my solution:
    http://stackoverflow.com/questions/1097839/bind-dictionary-to-itemscontrol-in-c-wpf

    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.

    Cheers

1 Trackback(s)

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

Post a Comment

Write Article for Dev102

Write for Dev102!

We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution

Learn More