views:

387

answers:

3

I have a ListBox in a PopupControl. Problem is that while scrolling, Popup resizes to fit the actual widest element. How to avoid this resizing and adjust automatically to the widest element in the whole list? I tried to put it in a Grid but without succes.

A: 

ok this is the solution: Adding this property

<ListBox VirtualizingStackPanel.IsVirtualizing="False"

The resizing stops because now the Panel contains all elements and the width is adjusted respecting the widest one. With Virtualizing Panel, it's only a part of items displayed and the ListBox adjusts the widht to the actual visible widest element.

Disadvantade is, that we do not use Virtualizing Panel anymore (which is default on)

PaN1C_Showt1Me
But this solution is too slow for many items!
PaN1C_Showt1Me
A: 

Most WPF UIElement controls have a Width property which can be set to "Auto" so they will take up as much space as their widest element.

Mike J
I don't think that works in this case, as nothing has determined what the widest element is if virtualizing is turned on.
GreenReign
Exactly. With virtualization ON it won't work
PaN1C_Showt1Me
A: 

If you want to keep virtualizing on, you can set the Popup.Width to a constant.

Of course, to pick the right constant, you'll have to calculate (or at least guess) how wide each ListBoxItem will be, and pick the max. ... Usually it's not too hard to get a rough guess, based on your content.

GreenReign