views:

453

answers:

1

I have a simple Dictionary defined as ResultOptions = new Dictionary<char, string> and my viewmodel has a public property ResultCode of type char. My XAML is as follow:

<ComboBox ItemsSource="{Binding ResultOptions}"
                            DisplayMemberPath="Value"
                            SelectedValuePath="Key"
                            SelectedValue="{Binding ResultCode}" />

Everything is working great except the ComboBox is never initialized based on the value of SelectedValue. I'm sure the binding is correct since i'm able to see that the ResultCode in the viewmodel is set properly.

The strange thing is...if i change my model to have ResultCode to be of type 'string' and my dictionary is Dictionary<string,string>, then the combobox is initialized properly based on the SelectedValue.

What's am i missing? why is binding to Dictionary<string, string> works, but binding to Dictionary<char, string> doesn't?

+1  A: 

One difference between the two cases is that char is a value type, while string is a reference type. Most likely internally WPF is using objects for bindings, and every time the char is received, it is boxed into a new object, so a reference equality check fails.

I'd say just use a string as your key, since there isn't any way to get around how boxing works.

Andy
Thanks! We did decide to use string as the key for now.
Detached
You should mark this answer as accepted if it answered your question so that the question no longer shows up as not having an accepted answer.
Andy