Adding ComboBoxItem to ComboBox Issue in Silverlight 4

Posted by AlishahNovin on Stack Overflow See other posts from Stack Overflow or by AlishahNovin
Published on 2010-04-19T22:16:39Z Indexed on 2010/04/19 22:33 UTC
Read the original article Hit count: 1038

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!

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about combobox