WPF MVVM Binding Checkbox.IsChecked for named element in dictionary <string, bool>

I have a group of checkboxes that represent different variants of the same type (for my example, they are all different types of files). I feel that there is too much to bind each to a separate property in the ViewModel, and I would prefer to bind them all to one collection and use the binding syntax to bind each flag to a specific element in the collection by key. I'm trying to stick with the MVVM pattern, so I just don't want to be lazy and handle the Checked event or something like that.

Is there a way to express a path or key in a collection using WPF binding syntax? For example, if I open Dictionary<string, bool>, called FileTypes, as a public property in ViewModel, is there a way to bind one of the checkboxes to FileTypes ["aspx"]? What if I had a more complex data structure, for example Dictionary<string, Dictionary<string, int>>?

+5
source share
1 answer

You almost had it - just drop the quotes around the indexer argument:

{Binding FileTypes[aspx]}

A more complex data structure will simply require a sequence of indexers, for example.

{Binding Foo[aspx][Bar]}

, MSDN, WPF > Data > Data Binding > Binding Declarations Overview - " " , .

+3

All Articles