Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
.NET Framework
ItemsControl Class
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

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

Represents a control that can be used to present a collection of items.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/xaml/presentation

Visual Basic (Declaration)
<LocalizabilityAttribute(LocalizationCategory.None, Readability := Readability.Unreadable)> _
<StyleTypedPropertyAttribute(Property := "ItemContainerStyle", StyleTargetType := GetType(FrameworkElement))> _
<ContentPropertyAttribute("Items")> _
Public Class ItemsControl _
    Inherits Control _
    Implements IAddChild
Visual Basic (Usage)
Dim instance As ItemsControl
C#
[LocalizabilityAttribute(LocalizationCategory.None, Readability = Readability.Unreadable)]
[StyleTypedPropertyAttribute(Property = "ItemContainerStyle", StyleTargetType = typeof(FrameworkElement))]
[ContentPropertyAttribute("Items")]
public class ItemsControl : Control, IAddChild
Visual C++
[LocalizabilityAttribute(LocalizationCategory::None, Readability = Readability::Unreadable)]
[StyleTypedPropertyAttribute(Property = L"ItemContainerStyle", StyleTargetType = typeof(FrameworkElement))]
[ContentPropertyAttribute(L"Items")]
public ref class ItemsControl : public Control, 
    IAddChild
J#
/** @attribute LocalizabilityAttribute(LocalizationCategory.None, Readability = Readability.Unreadable) */
/** @attribute StyleTypedPropertyAttribute(Property = "ItemContainerStyle", StyleTargetType = FrameworkElement) */
/** @attribute ContentPropertyAttribute("Items") */
public class ItemsControl extends Control implements IAddChild
JScript
public class ItemsControl extends Control implements IAddChild
XAML Object Element Usage
<ItemsControl>
  Items
</ItemsControl>

Content Model: Adding a child to an ItemsControl object implicitly adds it to the ItemCollection for the ItemsControl object. For more information on the content model for ItemsControl, see Controls Content Model Overview.

Note that you use either the Items or the ItemsSource property to specify the collection that should be used to generate the content of your ItemsControl. When the ItemsSource property is set, the Items collection is made read-only and fixed-size. This means that you cannot add items to the collection directly.

When ItemsSource is in use, setting the property to nullNothingnullptra null reference (Nothing in Visual Basic) removes the collection and restores usage to Items, which will be an empty ItemCollection.

Each ItemsControl type has a corresponding container type. Container elements are the objects that contain the data items in the item collection. For example, for ListBox, the generated containers are ListBoxItem controls; for ComboBox, they are ComboBoxItem controls.

The ItemsControl generates its items through the IItemContainerGenerator interface. The ItemContainerGenerator property of the ItemsControl is of type ItemContainerGenerator, which implements the IItemContainerGenerator interface. Therefore, you can access the ItemContainerGenerator object associated with your ItemsControl using the ItemContainerGenerator property. For example, if you have a data-bound TreeView and you want to get a TreeViewItem based on its index or its associated data item, you can use the ItemContainerGenerator..::.ContainerFromIndex or the ItemContainerGenerator..::.ContainerFromItem method. Alternatively, you can use the ItemContainerGenerator..::.IndexFromContainer or the ItemContainerGenerator..::.ItemFromContainer method to get the index or data item associated with a given generated container element.

Apart from those usages, the IItemContainerGenerator interface is used in advanced scenarios. Typically, advanced applications that have their own implementation of a virtualizing panel call members of the interface.

The following example is designed to illustrate the function of the different styling and templating related properties provided by the ItemsControl. The ItemsControl in this example is bound to a collection of Task objects. For demonstration purposes, the styles and templates in this example are all declared inline.

<ItemsControl Margin="10"
              ItemsSource="{Binding Source={StaticResource myTodoList}}">
  <!--The ItemsControl has no default visual appearance.
      Use the Template property to specify a ControlTemplate to define
      the appearance of an ItemsControl. The ItemsPresenter uses the specified
      ItemsPanelTemplate (see below) to layout the items. If an
      ItemsPanelTemplate is not specified, the default is used. (For ItemsControl,
      the default is an ItemsPanelTemplate that specifies a StackPanel.-->
  <ItemsControl.Template>
    <ControlTemplate TargetType="ItemsControl">
      <Border BorderBrush="Aqua" BorderThickness="1" CornerRadius="15">
        <ItemsPresenter/>
      </Border>
    </ControlTemplate>
  </ItemsControl.Template>
  <!--Use the ItemsPanel property to specify an ItemsPanelTemplate
      that defines the panel that is used to hold the generated items.
      In other words, use this property if you want to affect
      how the items are laid out.-->
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <!--Use the ItemTemplate to set a DataTemplate to define
      the visualization of the data objects. This DataTemplate
      specifies that each data object appears with the Proriity
      and TaskName on top of a silver ellipse.-->
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <DataTemplate.Resources>
        <Style TargetType="TextBlock">
          <Setter Property="FontSize" Value="18"/>
          <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
      </DataTemplate.Resources>
      <Grid>
        <Ellipse Fill="Silver"/>
        <StackPanel>
          <TextBlock Margin="3,3,3,0"
                     Text="{Binding Path=Priority}"/>
          <TextBlock Margin="3,0,3,7"
                     Text="{Binding Path=TaskName}"/>
        </StackPanel>
      </Grid>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
  <!--Use the ItemContainerStyle property to specify the appearance
      of the element that contains the data. This ItemContainerStyle
      gives each item container a margin and a width. There is also
      a trigger that sets a tooltip that shows the description of
      the data object when the mouse hovers over the item container.-->
  <ItemsControl.ItemContainerStyle>
    <Style>
      <Setter Property="Control.Width" Value="100"/>
      <Setter Property="Control.Margin" Value="5"/>
      <Style.Triggers>
        <Trigger Property="Control.IsMouseOver" Value="True">
          <Setter Property="Control.ToolTip"
                  Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                          Path=Content.Description}"/>
        </Trigger>
      </Style.Triggers>
    </Style>
  </ItemsControl.ItemContainerStyle>
</ItemsControl>

The following is a screenshot of the example when it is rendered:

ItemsControl example screenshot

Two other style-related properties of the ItemsControl that are not shown here are GroupStyle and GroupStyleSelector.

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows.Media..::.Visual
        System.Windows..::.UIElement
          System.Windows..::.FrameworkElement
            System.Windows.Controls..::.Control
              System.Windows.Controls..::.ItemsControl
                System.Windows.Controls..::.HeaderedItemsControl
                System.Windows.Controls..::.TreeView
                System.Windows.Controls.Primitives..::.MenuBase
                System.Windows.Controls.Primitives..::.Selector
                System.Windows.Controls.Primitives..::.StatusBar
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 Vista

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0 SP1
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content      
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker