ItemsControl Class

Updated: February 2009

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/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

[ContentPropertyAttribute("Items")]
[LocalizabilityAttribute(LocalizationCategory.None, Readability = Readability.Unreadable)]
[StyleTypedPropertyAttribute(Property = "ItemContainerStyle", StyleTargetType = typeof(FrameworkElement))]
public class ItemsControl : Control, 
	IAddChild
<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 null 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.

NoteNote:

The ItemsControl does not support containing duplicate objects. Two objects are considered to be duplicates if the GetHashCode method for each object returns the same value. If you attempt to add duplicate objects to an ItemsControl, certain features of UI Automation or classes that inherit from Selector might not work correctly. Additionally, the value that is returned by GetHashCode should not change for objects that are in an ItemsControl.

Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Themes.

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.Primitives.MenuBase
                System.Windows.Controls.Primitives.Selector
                System.Windows.Controls.Primitives.StatusBar
                System.Windows.Controls.TreeView

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 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

Date

History

Reason

February 2009

Described how default styles change dependency properties.

Customer feedback.

July 2008

Added new members: AlternationCount property, AlternationIndex property, ItemBindingGroup property, ItemStringFormat property, GetAlternationIndex method, OnAlternationCountChanged method, OnItemBindingGroupChanged method, OnItemStringFormatChanged method, AlternationCountProperty field, AlternationIndexProperty field, ItemBindingGroupProperty field, ItemStringFormatProperty field.

SP1 feature change.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft