水平定位的ListBoxItems不会拉伸

时间:2012-07-17 16:50:49

标签: wpf xaml layout listbox

我在使用WrapPanel和Orientation =“Horizo​​ntal”时尝试拉伸ListBoxItems:

<ListBox HorizontalContentAlignment="Stretch" ItemsSource="{Binding SomeCollection}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border>
                <!--Some Textboxes and Labels-->
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

如果我不使用WrapPanel,它将扩展ListBoxItems以匹配ListBox的大小。当我使用WrapPanel时,ListBoxItems具有最小宽度。

简言之:

我有一个列表,其中包含两个水平定向的ListBoxItem:

A list with two Items

当我展开主窗口时,ListBox也将展开,因为我有Horizo​​ntalAlignment =“Stretch”,但ListBoxItems不会。 enter image description here

所以我想要的是使用ListBox扩展的ListBoxItems,如下例所示:

The same list after I expanded the Window

对于这种情况,除ListBox之外是否有更好的控件? 如果不够清楚,请告诉我。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

你不能使用水平包裹面板,也期望元素水平拉伸,这是利益的对比矛盾。实际上,如果您希望任何类型的拉伸,WrapPanel可能不是正确的面板。

如果你希望它们在占据所有水平空间的同时占用相等的空间,则使用UniformGrid(将Rows设置为1)。

答案 1 :(得分:0)

您应该可以使用UniformGrid作为ItemsPanel,如此:

<ListBox HorizontalContentAlignment="Stretch" ItemsSource="{Binding SomeCollection}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border>
                <!--Some Textboxes and Labels-->
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="1" Columns="2" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>