ItemsControl.ItemsPanel 属性

定义

获取或设置模板,它定义了控制项的布局的面板。Gets or sets the template that defines the panel that controls the layout of items.

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; }
member this.ItemsPanel : System.Windows.Controls.ItemsPanelTemplate with get, set
Public Property ItemsPanel As ItemsPanelTemplate

属性值

一个 ItemsPanelTemplate,它定义了用于项的布局的面板。An ItemsPanelTemplate that defines the panel to use for the layout of the items. ItemsControl 的默认值是一个指定 ItemsPanelTemplateStackPanelThe default value for the ItemsControl is an ItemsPanelTemplate that specifies a StackPanel.

属性

示例

若要创建水平ListBox,可以创建一个指定水平StackPanel的模板, ItemsPanel并将其设置为属性。To create a horizontal ListBox, you can create a template that specifies a horizontal StackPanel and set it as the ItemsPanel property. 下面的示例演示一个ListBox Style创建水平ListBox的。The following example shows a ListBox Style that creates a horizontal ListBox.

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

下面的示例使用ControlTemplate创建具有圆角的水平。 ListBoxThe following example uses a ControlTemplate to create a horizontal ListBox that has rounded corners. 在此示例中,在中指定ItemsPanel ControlTemplate水平StackPanel ,而不是像在上一个示例中那样设置属性。In this example, instead of setting the ItemsPanel property as in previous example, the horizontal StackPanel is specified within the ControlTemplate. 在上true IsItemsHost ,属性设置为,指示生成的项应StackPanel在面板中。The IsItemsHost property is set to true on the StackPanel, indicating that the generated items should go in the panel. ItemsPanel 如果ControlTemplate以这种方式指定此方法,则不能在不使用的情况下由控件的用户替换。When you specify it this way, the ItemsPanel cannot be replaced by the user of the control without using a ControlTemplate. 因此,只有在不使用模板的情况下,才需要替换面板。Therefore, only do this if you know you would not want the panel to be replaced without the use of a template.

<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>

或者,您可以执行以下操作来获得相同的结果。Alternatively, you can do the following to achieve the same results. 在这种情况下ItemsPresenter ,会根据指定ItemsPanelTemplate的内容,创建项布局的面板。In this case, the ItemsPresenter creates the panel for the layout of the items based on what is specified by the 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>

注解

对于,默认值ItemsPanelTemplate指定VirtualizingStackPanelListBoxFor the ListBox, the default ItemsPanelTemplate specifies the VirtualizingStackPanel. 对于MenuItem,默认使用WrapPanelFor MenuItem, the default uses WrapPanel. 对于StatusBar,默认使用DockPanelFor StatusBar, the default uses DockPanel.

若要影响中ItemsControl的项的布局,可使用此属性ItemsPanelTemplate指定。To affect the layout of the items in an ItemsControl, you use this property to specify a ItemsPanelTemplate.

为视觉对象自定义提供了极大的灵活性,并提供了许多样式设置和模板化属性。ItemsControlThe ItemsControl provides great flexibility for visual customization and provides many styling and templating properties. 您可以使用ItemContainerStyle属性ItemContainerStyleSelector或属性来设置样式,以影响包含数据项的元素的外观。You use the ItemContainerStyle property or the ItemContainerStyleSelector property to set a style to affect the appearance of the elements that contain the data items. 例如,对于ListBox,生成的容器是ListBoxItem控件; 对于ComboBox,它们是ComboBoxItem控件。For example, for ListBox, the generated containers are ListBoxItem controls; for ComboBox, they are ComboBoxItem controls. 如果对控件使用分组,则可以使用GroupStyleGroupStyleSelector属性。If you are using grouping on your control, you can use the GroupStyle or GroupStyleSelector property. 若要指定数据对象的可视化,请使用ItemTemplate ItemTemplateSelector或属性。To specify the visualization of the data objects, use the ItemTemplate or the ItemTemplateSelector property. 有关详细信息,请参阅 数据模板化概述For more information, see Data Templating Overview.

依赖项属性信息Dependency Property Information

标识符字段Identifier field ItemsPanelProperty
元数据属性设置为trueMetadata properties set to true NoneNone

适用于

另请参阅