posts - 76, comments - 26, trackbacks - 0

Silverlight ComboBox SelectedItem Not Working

I spent well over an hour on Friday with this one and gave up.  I spent almost another hour today working on this.  I almost gave up, but when I’m in learn-mode like I am with Silverlight, these types of issues can really teach you a lot if you see them through.  I had a ComboBox with bindings on it’s ItemSource and SelectedItem like so:

            <ComboBox Name="ComboBoxControl" Style="{StaticResource ComboBoxStyle}"
                SelectedItem="{Binding Path=SelectedOption, Mode=TwoWay}"
                ItemsSource="{Binding Path=Options}"
                IsEnabled="{Binding Path=IsReadOnly}"
                Foreground="{StaticResource SemiDarkTextBrush}"                    
                Height="22" Width="150" Grid.Column="1" Margin="30,0,10,0" HorizontalAlignment="Stretch" >
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock 
                            Foreground="{StaticResource SemiDarkTextBrush}"                    
                            Text="{Binding Path=Label, Mode=OneWay}" 
                            TextWrapping="Wrap" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

 

I put breakpoints in my model’s SelectedOption property and I could see Silverlight calling it.  So, I knew there were no hard binding errors.  The bindings were working, except… they weren’t.  The ComboBox always showed up with no selected item.  I kept plugging away, and by accident I discovered the order of the binding occurrences matter.  Changing the markup to define the ItemsSource binding before the SelectedItem binding solved the problem.  Like so:

            <ComboBox Name="ComboBoxControl" Style="{StaticResource ComboBoxStyle}"
                ItemsSource="{Binding Path=Options}"
                SelectedItem="{Binding Path=SelectedOption, Mode=TwoWay}"
                IsEnabled="{Binding Path=IsReadOnly}"
                Foreground="{StaticResource SemiDarkTextBrush}"                    
                Height="22" Width="150" Grid.Column="1" Margin="30,0,10,0" HorizontalAlignment="Stretch" >
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock 
                            Foreground="{StaticResource SemiDarkTextBrush}"                    
                            Text="{Binding Path=Label, Mode=OneWay}" 
                            TextWrapping="Wrap" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

 

Wow, that’s pretty nasty.  I’m lucky stumbled on the solution.  I consider this a bug.

Hope this helps someone else out there

Print | posted on Monday, November 23, 2009 12:31 PM | Filed Under [ silverlight ]

Feedback

Gravatar

# re: Silverlight ComboBox SelectedItem Not Working

Thanks!! I've been struggling with this for way too long. Changed the order and it magically started working.

This absolutely has to be a bug.
2/2/2010 3:10 PM | Mark
Gravatar

# re: Silverlight ComboBox SelectedItem Not Working

Glad it helped somebody... I'm too lazy to log a bug, but I hope somebody does.
2/2/2010 3:45 PM | kellyb

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 1 and 5 and type the answer here:

Powered by: