WPF: WrapPanel in ItemsPanelTemplate expands list width
I have a listbox defined like this:
<ListBox.ItemTemplate>
<DataTemplate>
<ItemsControl>
<!-- Contents here -->
</ItemsControl>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My problem is this: This list is contained in a grid control, and should use all the available sp开发者_运维技巧ace of that cell its contained in, but it shouldn't force the parent to allocate MORE space. But what happens is that once the wrap panel fills up, instead of actually wrapping the items to the next line (as it should), it just expands the width of the listbox, and in the process forces the parent grid to resize as well.
How can I get the wrap panel to respect the size of its parent, and not force it to expand its size?
Thanks in advance!
edit: One more thing. I can set the width of the wrappanel explicitly to make it wrap, but I would like the wrappanel to have the same size as the listbox.
Set ScrollViewer.HorizontalScrollBarVisibility="Disabled"
and the WrapPanel
will wrap.
<Grid>
<ListBox
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
I've solved the problem.. the problem was (probably) that some of the containers that contained my listbox was a little too generous with allocating space for my listbox. In any case, as soon as I removed the scrollbar for the rest of my containers (that contains my list), it works as expected. :)
Thanks for your reply, it was very helpful in solving this.
Normally it won't wrap as you expect until you set the Horizontal scroll bar to disabled. Now you have a wrappanel that actually wraps :)
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
I was dealing with this issue in design mode (Blend and VS 2013) and it did not wrap until I added some design time sizing to the Page:
d:DesignHeight="300" d:DesignWidth="600"
精彩评论