System.Windows.Controls Nam ...


.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)
Syntax

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 .../>
Remarks

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.

Examples

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

System..::.Object
  System.Windows..::.DependencyObject
    System.Windows..::.UIElement
      System.Windows..::.FrameworkElement
        System.Windows.Controls..::.ItemsPresenter
Thread Safety

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

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

See Also

Reference

Tags :


Page view tracker