ItemsControl.ItemsPanel 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置模板,该模板定义对项的布局进行控制的面板。
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 的默认值是一个指定 StackPanel 的 ItemsPanelTemplate。
- 属性
示例
若要创建水平 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>
以下示例使用 a ControlTemplate 创建具有圆角的水平 ListBox 。 In this example, instead of setting the ItemsPanel property as in previous example, the horizontal StackPanel is specified within the ControlTemplate. 此属性 IsItemsHost 设置为 true
on, StackPanel指示生成的项应位于面板中。 When you specify it this way, the ItemsPanel cannot be replaced by the user of the control without using a ControlTemplate. 因此,仅当知道不希望在不使用模板的情况下替换面板,才执行此操作。
<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 控件。 如果在控件上使用分组,则可以使用 GroupStyle 或 GroupStyleSelector 属性。 若要指定数据对象的可视化效果,请使用 ItemTemplate 或 ItemTemplateSelector 属性。 有关详细信息,请参阅 数据模板化概述。
依赖项属性信息
标识符字段 | ItemsPanelProperty |
元数据属性设置为 true |
无 |