views:

1556

answers:

2

There are various posts on the web about this issue whereby the ComboBox only changes its dropdown height to fit the items on its very first dropdown. Any changes to the items shown doesn't cause the dropdown to resize. The various workarounds I have found don't seem to work for me, so I was wondering if anyone had found a way to solve this.

My current option is to use MinHeight and set it to a reasonable size (if I set MinHeight and MaxHeight, I get the same issue as if I set Height - no scrollbar when the items go outside the bounds.

I've tried changing the container for the items, but it seems they all have this issue. Any ideas?

Note that I have also tried programmatically recreating the combo whenever I change the ItemsSource as indicated on several forums, but I can't get this to work without crashing.

+1  A: 

The 'add and remove' method works for me. Here's what I do (in case you're doing something slightly different, or anyone else wants to try this method):


  • Read and store locally all the properties you're interested in
  • Remove the combo-box from the visual tree
  • Set the variable to null
  • Create a new combo box
  • Restore the properties you stored above
  • Take away the number you first thought of
  • Add it back into the visual tree

For example:

string lName = lComboBox.Name;
DataTemplate lTemplate = lComboBox.ItemTemplate;
Thickness lMargin = lComboBox.Margin;
// Other properties

LayoutParent.Children.Remove(lComboBox);

lComboBox= null;
lComboBox= new ComboBox(){
    Name = lName,
    ItemTemplate = lTemplate,
    Margin = lMargin,
    ItemsSource = lList // Your datasource
};

LayoutParent.Children.Add(lComboBox);
Mark Pim
Thanks, I'll give this a try - it's been frustrating the hell out of me as I know it shouldn't be so difficult.
Jeff Yates
Dear God, please tell me that this isn't the only way!
Jarrett Meyer
Other than writing your own combo...yes, it seems that way.
Jeff Yates
I don't know if the situation has improved in Silverlight 3 as I haven't used it yet. This is just one reason to avoid Silverlight for anything serious at present...
Mark Pim
See my recent solution below.
Jeff Yates
+1  A: 

I found a great solution thanks to this answer. It works great and isn't quite as hacky as other suggestions (effective as they are).

Obviously, community wiki-ing this post as it isn't my answer. Please go vote for the real answer, markti deserves double rep for this one.

Jeff Yates