views:

593

answers:

1

I recently upgraded my Silverlight app from 3 to 4. After a few hours of bashing my head against the wall, trying to resolve an issue, I've narrowed down the problem to this:

I have a user control, with a ComboBox inside it. The ComboBox has a single ComboBoxItem child. The user control exposes a get accessors that returns the ComboBox's Items object, allowing me to add additional ComboBoxItems via xaml.

This all worked fine in Silverlight 3, however it's not working in Silverlight 4.

As code:

//XAML
<UserControl ... >
  <ComboBox Name="myComboBox">
    <ComboBoxItem Content="Select an Item" />
  </ComboBox>
  <!-- All my other stuff -->
</UserControl>

//Code behind
public ItemCollection ListItems
{
   get
   {
     return myComboBox.Items;
   }
}

//Implementation of User-Control
<CustomControl:UserControl ... >
  <CustomControl:UserControl.ListItems>
    <ComboBoxItem Content="Item 1" />
    <ComboBoxItem Content="Item 2" />
    <ComboBoxItem Content="Item 3" />
  </CustomControl:UserControl.ListItems>
</CustomControl:UserControl>

As I mentioned, this all worked fine in Silverlight 3, but doesn't work in Silverlight 4.

The workaround, it seems, is to remove that single ComboBoxItem that's inside my user-control, but I'm hoping to avoid, as I want it as the default item.

Any help would be much appreciated!

+1  A: 

The XAML parser was re-written for Silverlight 4 to make it more consistent with WPF. I am pretty certain the behavior you are expecting was a bug in SL3 and I don't think it would have worked that way in WPF though I've never actually tried.

You may be able to get the old mode back by enabling quirks mode but I wouldn't recommend it. Instead, what I would do is create a ControlTemplate for the combo box to display the "select an item" text when there is nothing selected. Having that be an actual item in the combo box really just a hack that we've always been forced to do with technologies such as Windows Forms and HTML but in Silverlight it seems like having SelectedItem be null is more appropriate.

Josh Einstein
Thanks for that!
AlishahNovin