我试图在使用WrapPanel和Orientation=“水平”时伸展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的宽度最小。
简要地说:
我有一个列表,上面有两个水平方向的ListBoxItems:

当我展开主窗口时,ListBox也会展开,因为我有HorizontalAlignment="Stretch“,但ListBoxItems不会。

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

对于这种情况,有没有比ListBox更好的控件?如果这还不够清楚,请告诉我。谢谢你的帮助。
发布于 2012-07-18 06:23:38
您应该能够使用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>发布于 2012-07-18 01:52:54
您不能在使用水平包裹面板的同时还期望元素水平拉伸,这是一个逻辑上的利益冲突。实际上,如果你想要任何类型的伸展,WrapPanel可能不是正确的面板。
如果你想让它们占用相等的空间,同时占用所有的水平空间,那么可以使用UniformGrid (将Rows设置为1)。
https://stackoverflow.com/questions/11527185
复制相似问题