I'd like to bind a Dictionary<string, int>
to a ListView
in WPF. I'd like to do this in such a way that the Values
in the Dictionary
get updated via the data binding mechanism. I don't want to change the Keys
just the Values
. I also don't care about adding new mappings to the Dictionary
. I just want to update existing ones.
Setting the Dictionary as the ItemsSource
of the ListView
doesn't accomplish this. It doesn't work because the ListView
uses the Enumerator to access the contents of the Dictionary
and the elements of that enumeration are immutable KeyValuePair
objects.
My current line of investigation attempts to use the Keys
property. I assign this to the ItemsSource
property of my ListView
. This does allow me to display the Keys
but I don't know enough about WPF's databinding mechanism to access the Values
in the Dictionary
.
I found this question: Access codebehind variable in XAML but still can't seem to bridge the gap.
Do any of you know how to make this approach work? Does anyone have a better approach?
It seems like, as a last resort, I could build a custom object and stick it in a List
from which I recreate/update my Dictionary
but this seems like a way to circumvent the built-in data binding functionality rather than effectively utilize it.