Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ItemsPanelTemplate Class

Specifies the panel that the ItemsPresenter creates for the layout of the items of an ItemsControl.

Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public class ItemsPanelTemplate : FrameworkTemplate
public class ItemsPanelTemplate extends FrameworkTemplate
public class ItemsPanelTemplate extends FrameworkTemplate
<ItemsPanelTemplate>
  VisualTree
</ItemsPanelTemplate>

The ItemsPanelTemplate specifies the panel that is used for the layout of items. GroupStyle has a Panel property that is of type ItemsPanelTemplate. ItemsControl types have an ItemsPanel property that is of type ItemsPanelTemplate.

Each ItemsControl type has a default ItemsPanelTemplate. For the ItemsControl class, the default ItemsPanel value is an ItemsPanelTemplate that specifies a StackPanel. For the ListBox, the default uses the VirtualizingStackPanel. For MenuItem, the default uses WrapPanel. For StatusBar, the default uses DockPanel.

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. Note that in this example, instead of setting the ItemsPanel property as in previous example, the horizontal StackPanel is specified within the ControlTemplate. Note that 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 wouldn't 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 an example of how to use the different styling and templating properties on the ItemsControl, see Data Templating Overview.

System.Object
   System.Windows.Threading.DispatcherObject
     System.Windows.FrameworkTemplate
      System.Windows.Controls.ItemsPanelTemplate

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft