0 out of 1 rated this helpful - Rate this topic

ItemContainerGenerator Class

Generates the user interface (UI) on behalf of its host, such as an ItemsControl.

System.Object
  System.Windows.Controls.ItemContainerGenerator

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
public sealed class ItemContainerGenerator : IRecyclingItemContainerGenerator, 
	IItemContainerGenerator, IWeakEventListener

The ItemContainerGenerator type exposes the following members.

  Name Description
Public property Status The generation status of the ItemContainerGenerator.
Top
  Name Description
Public method ContainerFromIndex Returns the element corresponding to the item at the given index within the ItemCollection.
Public method ContainerFromItem Returns the UIElement corresponding to the given item.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method IndexFromContainer Returns the index to an item that corresponds to the specified, generated UIElement.
Public method ItemFromContainer Returns the item that corresponds to the specified, generated UIElement.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Public event ItemsChanged The ItemsChanged event is raised by a ItemContainerGenerator to inform layouts that the items collection has changed.
Public event StatusChanged The StatusChanged event is raised by a ItemContainerGenerator to inform controls that its status has changed.
Top
  Name Description
Explicit interface implemetation Private method IItemContainerGenerator.GenerateNext() Returns the container element used to display the next item.
Explicit interface implemetation Private method IItemContainerGenerator.GenerateNext(Boolean) Returns the container element used to display the next item, and whether the container element has been newly generated (realized).
Explicit interface implemetation Private method IItemContainerGenerator.GeneratorPositionFromIndex Returns the GeneratorPosition object that maps to the item at the specified index.
Explicit interface implemetation Private method IItemContainerGenerator.GetItemContainerGeneratorForPanel Returns the ItemContainerGenerator appropriate for use by the specified panel.
Explicit interface implemetation Private method IItemContainerGenerator.IndexFromGeneratorPosition Returns the index that maps to the specified GeneratorPosition.
Explicit interface implemetation Private method IItemContainerGenerator.PrepareItemContainer Prepares the specified element as the container for the corresponding item.
Explicit interface implemetation Private method IItemContainerGenerator.Remove This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.
Explicit interface implemetation Private method IItemContainerGenerator.RemoveAll Removes all generated (realized) items.
Explicit interface implemetation Private method IItemContainerGenerator.StartAt(GeneratorPosition, GeneratorDirection) Prepares the generator to generate items, starting at the specified GeneratorPosition, and in the specified GeneratorDirection.
Explicit interface implemetation Private method IItemContainerGenerator.StartAt(GeneratorPosition, GeneratorDirection, Boolean) Prepares the generator to generate items, starting at the specified GeneratorPosition, and in the specified GeneratorDirection, and controlling whether or not to start at a generated (realized) item.
Explicit interface implemetation Private method IRecyclingItemContainerGenerator.Recycle This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.
Explicit interface implemetation Private method IWeakEventListener.ReceiveWeakEvent This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.
Top

ItemContainerGenerator implements IItemContainerGenerator, which is the interface that defines a type which:

  • Maintains an association between the data view of a multiple-item control, such as ContainerFromElement and the corresponding UIElement items.

  • Generates UIElement items on behalf of a multiple-item control.

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.

If you need to get a specific ListBoxItem at a particular index in a ListBox, you can use an ItemContainerGenerator.

The following example shows a ListBox and its items.


<ListBox Margin="10,0,0,5" Name="lb" VerticalAlignment="Top" Grid.Column="0" Grid.Row="2">
    <ListBoxItem>Item 0</ListBoxItem>
    <ListBoxItem>Item 1</ListBoxItem>
    <ListBoxItem>Item 2</ListBoxItem>
    <ListBoxItem>Item 3</ListBoxItem>
</ListBox>


The following example shows how to retrieve the item by specifying the index of the item in the ContainerFromIndex property of the ItemContainerGenerator.


private void GetIndex0(object sender, RoutedEventArgs e)
{
  ListBoxItem lbi = (ListBoxItem)
      (lb.ItemContainerGenerator.ContainerFromIndex(0));
  Item.Content = "The contents of the item at index 0 are: " +
      (lbi.Content.ToString()) + ".";
}


After you have retrieved the list box item, you can display the contents of the item, as shown in the following example.


Item.Content = "The contents of the item at index 0 are: " +
    (lbi.Content.ToString()) + ".";


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ