We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution
Learn More
Binding 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 :
.NetBindingBinding ExpressionC#ComboboxControl BindingWPF Copyright © 2012 Dev102.com
Breeze : Designed by Amit Raz and Nitzan Kupererd
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!
Martin
Martin Cook
Said on June 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
Martin Cook
Said on June 19, 2008 :
Heh, cant post XAML then.
Ah well.
Mario
Said on June 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
Joseph Melettukunnel
Said on July 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
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?
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;
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: