【问题标题】:Silverlight: Set Items Widths in ItemsControl to StretchSilverlight:将 ItemsControl 中的项目宽度设置为 Stretch
【发布时间】:2010-03-22 11:25:58
【问题描述】:

我有一个从上到下填充的 ItemsControl,但我不能让它的子项占据 ItemsControl 的整个宽度:

我基本上需要拉伸绿色位以填充控件的宽度(如蓝色位所示)。

我尝试过将模板项的HorizontalAlignment 属性设置为Stretch,甚至尝试将其Width 属性绑定到ItemsControl Width,但都没有成功。

应该是直截了当的,但这是我无法弄清楚的事情......

编辑:这里是 ItemTemplate(整个就是 ItemTemplate,它本身包含一个绑定到子对象列表的 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 会有帮助吗?
  • 好吧,现在我很困惑,你为什么要使用 WrapPanel,而你只是想要一个直接的列表?
  • 这是给子 ItemsControl 的,它包含 n 个需要包装的项目。
  • 那么外层ItemsControl的Panel类型是什么?

标签: silverlight itemscontrol html


【解决方案1】:

您需要配置ListBoxItemHorizontalContentAlignment,您可以通过在ListBoxItemContainerStyle 中使用Style 对象来执行此操作,如下所示:-

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

【讨论】:

  • 谢谢,但我真的不想使用 ListBox,因为不需要选择
【解决方案2】:

好的,所以当我继续构建应用程序时,我想出了如何解决这个问题。本质上,当我更改ItemsControl 的模板以支持滚动时,项目跨越以水平填充:)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多