Tuesday, April 24, 2012

'String' object cannot be added to ComboBox, WPF

Friends If you got stuck with this error while working on your WPF project then you'll absolutely smile after reading this post.

I was working on a task which involved creating new ComboBox and several complex binding and all but after some time I got stuck with an exception which was:
{"'String' object cannot be added to 'ComboBox'. Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.  Error at object 'System.Windows.Controls.ComboBox' in markup file '<Project Name>;<ComponentName>' Line 3180 Position 108."}
 And I spent nearly 4-5 hours to debug this issue, moreover you need to dig up in order to come to the conclusion that this is the exception which is causing issue to you and after lots of researching over net (which in fact dint give me a single link about this exception) I called up my colleague and he just saw my code and smiled- in fact I also smiled as soon as we both started to look into the code.
<Combobox x:Name="abcMyCombo"
                   ItemsSource="abcCollection"
                   SelectedItem="abcd"
                   .... Many other properties
                  >>
</Combobox>
 I hope you've just found the issue with it :). In case not notice >> in the end, Combobox was trying to add this as a string + its itemsource. I'll be more careful next time while writing xaml. Funny isn't?

 

No comments:

Post a Comment