2

I have a Dictionary in my C# code behind of a WPF application. I wanted to bind a Listbox to the Keys of that Dictionary, and that works.

But I'd also obviously like it to update whenever the Dictionary has new elements, or when some are deleted (I thought it was the basic usage of Binding oO)... and I got troubles with this because as far as I understood browsing the net, I should use a ObersvableCollection, which does not exist for dictionaries =/

Plus I'd very much prefer to use the dictionary as it is.

It looks like this:

<ListBox Margin="5" 
         Name="listBoxAvailableReplays" 
         Background="Transparent" 
         ItemsSource="{Binding}" 
         DataContext="{Binding UpdateSourceTrigger=PropertyChanged}" 
/>

And the code-behind (that code is in the constructor):

listBoxAvailableReplays.DataContext = 
          m_IFileHandlingBaboon.AvailableReplays.Keys;
slavoo
  • 5,798
  • 64
  • 37
  • 39
Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88

1 Answers1

2

@FatalBaboon,

There doesn't seem to be a simple solution (requires creating a custom observable dictionary).

Related:
General Observable Dictionary Class for DataBinding/WPF C#
Two Way Data Binding With a Dictionary in WPF
Bind observable dictionary to listbox items. [WPF-C#]
http://drwpf.com/blog/2007/09/16/can-i-bind-my-itemscontrol-to-a-dictionary/

Community
  • 1
  • 1
publicgk
  • 3,170
  • 1
  • 26
  • 45
  • I think I'm gonna try to make drwpf's ObservableDictionary. As far as I understood I have to declare my ObservableDictionary as a Resource then bind to the keys. – Louis Kottmann Apr 06 '11 at 15:02