Thursday, July 30, 2009

ComboBox In Silverlight 3 And Binding

While recent exploration of Silverlight 3 I've stuck with the problem with following code:

<ComboBox SelectedItem="{Binding Path=Region}" ItemsSource="{Binding Path=Region.ParentCollection}" DisplayMemberPath="Name" />

It's not a good piece of code from design point of view: city references region in which it is located and region has a reference to collection of all available regions. And the problem with that code is that combo box shows list of regions but it doesn't select region in the box. Setting SelectedIndex to constant value also cause ArgumentOutOfRangeException. After several trials and errors I've found that if I firstly assign ItemsSource attribute in XAML and only then SelectedItem ComboBox works correctly! It seems that order of attributes in Silverlight 3 XAML matters. I've never seen that order of attributes may be important. As I know XSD doesn't allow to define strict order of attributes (but this can be done for the elements), so this looks very ridiculous.

Or may be my tricky and ugly code was the cause of such strange behaviour of framework? Hope in future I'll found answer to this question.