Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
Silverlight 3
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Silverlight 3

Other versions are also available for the following:
.NET Framework Class Library for Silverlight
ItemsPresenter Class

Specifies where items are placed in a control, usually an ItemsControl.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
Public NotInheritable Class ItemsPresenter _
    Inherits FrameworkElement
Visual Basic (Usage)
Dim instance As ItemsPresenter
C#
public sealed class ItemsPresenter : FrameworkElement
XAML Object Element Usage
<ItemsPresenter .../>

Use the ItemsPresenter in the ControlTemplate of a control to specify the place in the control’s visual tree where the ItemsPanel is added. The ItemsPanel specifies the panel that is used for the layout of items in the control.

The following example creates a ControlTemplate for a ListBox and uses an ItemsPresenter to determine where the items in the ListBox are placed.

XAML
<Grid>
  <Grid.Resources>
    <Style x:Key="ListBoxTemplate" TargetType="ListBox">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="ListBox">
            <Grid>
              <Ellipse Stroke="{TemplateBinding BorderBrush}"
                  StrokeThickness="{TemplateBinding BorderThickness}"
                  Fill="{TemplateBinding Background}"/>
              <ScrollViewer HorizontalScrollBarVisibility="Auto"
                          VerticalScrollBarVisibility="Auto">
                <ItemsPresenter HorizontalAlignment="Center"
                              VerticalAlignment="Center"/>
              </ScrollViewer>
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

    <src:Items x:Key="myItems"/>
  </Grid.Resources>

  <ListBox ItemsSource="{StaticResource myItems}" 
         Style="{StaticResource ListBoxTemplate}"
         Width="250" Height="250" 
         Background="Aquamarine" BorderBrush="Navy" BorderThickness="5"/>
</Grid>

The following example shows the collection of strings to which the ListBox is bound.

Visual Basic
Public Class Items
    Inherits System.Collections.ObjectModel.ObservableCollection(Of String)

    Public Sub New()
        Add("Item 1")
        Add("Item 2")
        Add("Item 3")
        Add("Item 4")
        Add("Item 5")
    End Sub
End Class
C#
public class Items : 
    System.Collections.ObjectModel.ObservableCollection<string>
{
    public Items()
    {
        Add("Item 1");
        Add("Item 2");
        Add("Item 3");
        Add("Item 4");
        Add("Item 5");
    }
}

The preceding example produces output that is similar to the following illustration.

ListBox with a custom ControlTemplate

ListBox with new control template
System..::.Object
  System.Windows..::.DependencyObject
    System.Windows..::.UIElement
      System.Windows..::.FrameworkElement
        System.Windows.Controls..::.ItemsPresenter
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker