Share via


ItemsControl.ItemsPanel 属性

定义

获取或设置模板,该模板定义对项的布局进行控制的面板。

public:
 property System::Windows::Controls::ItemsPanelTemplate ^ ItemsPanel { System::Windows::Controls::ItemsPanelTemplate ^ get(); void set(System::Windows::Controls::ItemsPanelTemplate ^ value); };
[System.ComponentModel.Bindable(false)]
public System.Windows.Controls.ItemsPanelTemplate ItemsPanel { get; set; }
[<System.ComponentModel.Bindable(false)>]
member this.ItemsPanel : System.Windows.Controls.ItemsPanelTemplate with get, set
Public Property ItemsPanel As ItemsPanelTemplate

属性值

一个 ItemsPanelTemplate,它定义要用于项的布局的面板。 ItemsControl 的默认值是一个指定 StackPanelItemsPanelTemplate

属性

示例

若要创建水平 ListBox,可以创建一个指定水平 StackPanel 的模板,并将其设置为 ItemsPanel 属性。 下面的示例演示了创建 ListBoxStyle 水平 ListBox的 。

<Style TargetType="ListBox">
  <Setter Property="ItemsPanel">
    <Setter.Value>
      <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center"/>
      </ItemsPanelTemplate>
    </Setter.Value>
  </Setter>
</Style>

以下示例使用 ControlTemplate 创建具有圆角的水平 ListBox 。 在此示例中,在 中ControlTemplate指定水平StackPanel,而不是像上一示例中那样设置 ItemsPanel 属性。 属性IsItemsHost在 上StackPanel设置为 true ,指示生成的项应进入面板中。 以这种方式指定时,如果不使用 ControlTemplateItemsPanel则控件的用户无法替换 。 因此,仅当知道不希望在不使用模板的情况下替换面板时,才执行此操作。

<Style TargetType="ListBox">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ListBox">
        <Border CornerRadius="5" Background="{TemplateBinding ListBox.Background}">
          <ScrollViewer HorizontalScrollBarVisibility="Auto">
            <StackPanel Orientation="Horizontal"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       IsItemsHost="True"/>
          </ScrollViewer>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

或者,可以执行以下操作来实现相同的结果。 在这种情况下, ItemsPresenter 会根据 指定的 ItemsPanelTemplate内容为项的布局创建面板。

<Style TargetType="{x:Type ListBox}">
  <Setter Property="ItemsPanel">
    <Setter.Value>
      <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal"
                     VerticalAlignment="Center"
                     HorizontalAlignment="Center"/>
      </ItemsPanelTemplate>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ListBox}">
        <Border CornerRadius="5"
                Background="{TemplateBinding ListBox.Background}">
          <ScrollViewer HorizontalScrollBarVisibility="Auto">
            <ItemsPresenter/>
          </ScrollViewer>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

注解

对于 , ListBox默认值 ItemsPanelTemplate 指定 VirtualizingStackPanel。 对于 MenuItem,默认值使用 WrapPanel。 对于 StatusBar,默认值使用 DockPanel

若要影响 中 ItemsControl项的布局,请使用此属性指定 ItemsPanelTemplate

ItemsControl 视觉对象自定义提供了极大的灵活性,并提供了许多样式设置和模板化属性。 使用 ItemContainerStyle 属性或 ItemContainerStyleSelector 属性设置样式以影响包含数据项的元素的外观。 例如,对于 ListBox,生成的容器是 ListBoxItem 控件;对于 ComboBox,它们是 ComboBoxItem 控件。 如果在控件上使用分组,则可以使用 GroupStyleGroupStyleSelector 属性。 若要指定数据对象的可视化效果,请使用 ItemTemplateItemTemplateSelector 属性。 有关详细信息,请参阅 数据模板化概述

依赖项属性信息

标识符字段 ItemsPanelProperty
元数据属性设置为 true

适用于