ItemsControl.ItemsPanel Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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; }
[<System.ComponentModel.Bindable(false)>]
member this.ItemsPanel : System.Windows.Controls.ItemsPanelTemplate with get, set
Public Property ItemsPanel As ItemsPanelTemplate
An ItemsPanelTemplate that defines the panel to use for the layout of the items. The default value for the ItemsControl is an ItemsPanelTemplate that specifies a StackPanel.
- Attributes
To create a horizontal ListBox, you can create a template that specifies a horizontal StackPanel and set it as the ItemsPanel property. 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>
The following example uses a ControlTemplate to create a horizontal ListBox that has rounded corners. In this example, instead of setting the ItemsPanel property as in previous example, the horizontal StackPanel is specified within the ControlTemplate. The IsItemsHost property is set to true
on the StackPanel, indicating that the generated items should go in the panel. 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. 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>
For the ListBox, the default ItemsPanelTemplate specifies the VirtualizingStackPanel. For MenuItem, the default uses WrapPanel. For StatusBar, the default uses DockPanel.
To affect the layout of the items in an ItemsControl, you use this property to specify a ItemsPanelTemplate.
The ItemsControl provides great flexibility for visual customization and provides many styling and templating properties. 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. For example, for ListBox, the generated containers are ListBoxItem controls; for ComboBox, they are ComboBoxItem controls. If you are using grouping on your control, you can use the GroupStyle or GroupStyleSelector property. To specify the visualization of the data objects, use the ItemTemplate or the ItemTemplateSelector property. For more information, see Data Templating Overview.
Identifier field | ItemsPanelProperty |
Metadata properties set to true |
None |
Product | Versions |
---|---|
.NET Framework | 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: