I'm still having difficulties understanding how the layout of a ListBox with a WrapPanel works, or is supposed to work. Here's a tiny example:
<Window x:Class="ListBoxWidth.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListBoxWidth" Height="300" Width="300"
>
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Hidden">
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>listbox item 1</ListBoxItem>
<ListBoxItem>listbox item 2</ListBoxItem>
<ListBoxItem>listbox item 3</ListBoxItem>
<ListBoxItem>listbox item 4</ListBoxItem>
<ListBoxItem>listbox item 5</ListBoxItem>
</ListBox>
</ScrollViewer>
</Grid>
</Window>
The ListBoxItems are laid out in one row, regardless of the window size. What seems to happen is:
- the ListBox asks the WrapPanel for its desired size
- the WrapPanel says "would be nice if I could lay out all these items horizontally, so there"
- the ListBox allocates this space to the internal scrollable area
- but the ListBox control itself is sized to fit into the window frame.
At least with the <ScrollViewer> around it, I would expect the ListBox to pass the available space to its children, and not allow for any scrolling. In the example, the scrollbar is hidden, but you can still scroll with the cursor keys.
I know that this can be worked around by setting WrapPanel.Width explicitly.
But: is the current behavior intended, or is it a bug? If it's intentional, what would be the correct, "officially supported" way to get what I want?
Thanks,
-Kolja
<Window x:Class="ListBoxWidth.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListBoxWidth" Height="300" Width="300"
>
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Hidden">
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>listbox item 1</ListBoxItem>
<ListBoxItem>listbox item 2</ListBoxItem>
<ListBoxItem>listbox item 3</ListBoxItem>
<ListBoxItem>listbox item 4</ListBoxItem>
<ListBoxItem>listbox item 5</ListBoxItem>
</ListBox>
</ScrollViewer>
</Grid>
</Window>
The ListBoxItems are laid out in one row, regardless of the window size. What seems to happen is:
- the ListBox asks the WrapPanel for its desired size
- the WrapPanel says "would be nice if I could lay out all these items horizontally, so there"
- the ListBox allocates this space to the internal scrollable area
- but the ListBox control itself is sized to fit into the window frame.
At least with the <ScrollViewer> around it, I would expect the ListBox to pass the available space to its children, and not allow for any scrolling. In the example, the scrollbar is hidden, but you can still scroll with the cursor keys.
I know that this can be worked around by setting WrapPanel.Width explicitly.
But: is the current behavior intended, or is it a bug? If it's intentional, what would be the correct, "officially supported" way to get what I want?
Thanks,
-Kolja