views:

43

answers:

2

Hi, 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 space 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.

+1  A: 

Set ScrollViewer.HorizontalScrollBarVisibility="Disabled" and the wrap panel will wrap.

<Grid>
    <ListBox
        ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
</Grid>
Wallstreet Programmer
Unfortunately that didn't work... I've tried to both set it to Hidden and Disabled, and it still expands the space it uses. :(Note that the problem was never that the listbox became larger than the grid it was in (so a scrollbar would show), the problem is that the grid cell it's in is enlarged, and the listbox with it.
Hallgeir
Oh, one more thing. The grid is contained in a uniformgrid, can that be the problem? Maybe uniformgrid doesn't contain the grid and because of that expands.
Hallgeir
In your question you mentioned wrappanel and not uniformgrid. My code fixes your problem as you explained it in your question about wrappanels. If you also have a problem with uniformgrid as itemspanel in listboxes, you should write another question.
Wallstreet Programmer
I think you misunderstand me... The question IS about wrappanel not wrapping.The structure is like this: UniformGrid->Grid->ListBox with WrapPanel
Hallgeir
+1  A: 

Hi, 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.

Hallgeir