4

Say I wanna bind to dictionary that TKey is string with XAML:

<Label DataContext="{MyDictionary}" Content="{Binding Item("OK")}" />

Doesn't work.

How should I do it?

I am talking about the Item("Key")

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632

1 Answers1

12

Try that :

<Label DataContext="{Binding MyDictionary}" Content="{Binding [OK]}" />

Or that (a bit simpler) :

<Label Content="{Binding MyDictionary[OK]}" />
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • 2
    well, I knew it was possible with list indexes, I wasn't sure about dictionary keys.. I just tried, and it worked ;) – Thomas Levesque Sep 14 '09 at 09:49
  • For me, the first suggestion does [not work](http://stackoverflow.com/questions/14623728/using-hashtable-as-datacontext-for-wpf-xaml-binding#comment20428624_14623728). – Rudey Feb 01 '13 at 08:23
  • 1
    This does not work when the key has spaces or other special characters like periods and slashes, though. I tried putting the key into double and single quotes but neither works. – Machisuji Feb 03 '19 at 14:42