I have a ComboBox in Silverlight that has very inconsistent behavior.
I have the ComboBox bound to a dynamic collection of data where elements are added or removed. Here is the XAML for the ComboBox:
<ComboBox Margin="0,-1,0,0" Width="20" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemsContained}" x:Name="TabComboBox" >
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Background="White" MinWidth="250" />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
So this works great and the ComboBox opens “up” with the long list of items in ItemsContained. However, if I delete one of the items in ItemsContained, the ComboBox all of a sudden will switch from opening “up” to opening “down” when you click on it, despite the fact there are a lot of items in this collection and there is only 20 or so pixels of room for it to open down. I cannot figure this out. I have even tried setting the ItemsPanelTemplate to have a MinHeight, but that does not help. Does anyone know how to make the ComboBox always open “up”?
Also, even if I set the MinHeight to something ridiculous, such as 10,000, it still does this.
EDIT: As an update, I have gotten this to work by creating a whole new ComboBox every time ItemsContained is changed. This is the code:
scrollingGrid.Children.Remove(tabComboBox);
tabComboBox.ItemsSource = null;
ComboBox boxy = new ComboBox()
{
ItemsSource = ItemsContained
};
scrollingGrid.Children.Add(boxy);
tabComboBox = boxy;
I feel this is a little ad hoc, so if anyone has a better idea, let me know. Changing the height of the ScrollViewer inside the ComboBox does not work either.
I think this is a Silverlight Bug. When you set the ItemsSource property the behavior of the combobox popup changes from up to down.
I Clone my combobox in the event DropDownClosed of each combobox and that works for me.