Silverlight:在ItemsControl中设置项的宽度为Stretch

3
我有一个ItemsControl,它从顶部到底部填充,但我无法让其子项占据整个ItemsControl的宽度:
我基本上需要拉伸绿色部分以填满控件的宽度(如蓝色部分所示)。
我尝试过设置模板项的HorizontalAlignment属性为Stretch,甚至尝试将其Width属性绑定到ItemsControl Width,但都没有起作用。
这应该是一个简单的问题,但我就是无法完全弄清楚...
编辑:这里是ItemTemplate(整个内容都是ItemTemplate,其中包含一个ItemsControl,该ItemsControl绑定到子对象列表):
<DataTemplate>
    <Border CornerRadius="5" Background="#ddd">
        <StackPanel>
            <TextBlock Text="{Binding Name}" FontSize="18" Foreground="#bbb"/>
            <ItemsControl ItemsSource="{Binding PlugIns}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel HorizontalAlignment="Stretch" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="10,0,10,10" Tag="{Binding}" 
                                MouseEnter="StackPanel_MouseEnter">
                            <Border Child="{Binding Icon}" />
                            <TextBlock Text="{Binding Name}" />
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>
    </Border>
</DataTemplate>

你的ItemTemplate会有帮助吗? - AnthonyWJones
好的,现在我有点糊涂了,为什么你想要一个直线列表时要使用WrapPanel? - AnthonyWJones
这是针对包含n个需要换行的子项的ItemsControl。 - CatBusStop
那么外部ItemsControl的Panel类型是什么? - AnthonyWJones
2个回答

3

您需要配置 ListBoxItemHorizontalContentAlignment,可以通过在 ListBoxItemContainerStyle 中使用 Style 对象来实现:

 <ListBox ....>
  <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
      <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
  </ListBox.ItemContainerStyle>

谢谢,但我不想使用ListBox,因为没有选择的必要。 - CatBusStop

1

好的,当我继续构建应用程序时,我找到了解决方法。本质上,当我更改ItemsControl的模板以支持滚动时,项目会横向填充 :)

<ItemsControl>
    <ItemsControl.Template>
        <ControlTemplate>
            <ScrollViewer Padding="{TemplateBinding Padding}">
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
    </ItemsControl.Template>
    ...
</ItemsControl>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接