Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

ItemContainerGenerator Class

Provides mappings between the items of an ItemsControl and their container elements.

System.Object
  System.Windows.Controls.ItemContainerGenerator

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

The ItemContainerGenerator type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneContainerFromIndexReturns the container for the item at the specified index within the ItemCollection.
Public methodSupported by Silverlight for Windows PhoneContainerFromItemReturns the container corresponding to the specified item.
Public methodSupported by Silverlight for Windows PhoneEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGeneratorPositionFromIndexGets the generated position of the item at the specified index.
Public methodSupported by Silverlight for Windows PhoneGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneIndexFromContainerReturns the index to the item that has the specified, generated container.
Public methodSupported by Silverlight for Windows PhoneIndexFromGeneratorPositionReturns the index that maps to the specified GeneratorPosition.
Public methodSupported by Silverlight for Windows PhoneItemFromContainerReturns the item that corresponds to the specified, generated container.
Protected methodSupported by Silverlight for Windows PhoneMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Public eventSupported by Silverlight for Windows PhoneItemsChangedOccurs when the contents of the items collection changes.
Top
  NameDescription
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneIItemContainerGenerator.GenerateNextReturns the container element used to display the next item, and whether the container element has been newly generated (realized).
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneIItemContainerGenerator.GetItemContainerGeneratorForPanelReturns the ItemContainerGenerator appropriate for use by the specified panel.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneIItemContainerGenerator.PrepareItemContainerPrepares the specified element as the container for the corresponding item.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneIItemContainerGenerator.RemoveRemoves one or more generated (realized) items.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneIItemContainerGenerator.RemoveAllRemoves all generated (realized) items.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneIItemContainerGenerator.StartAtPrepares 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 implemetationPrivate methodSupported by Silverlight for Windows PhoneIRecyclingItemContainerGenerator.RecycleDisassociates item containers from their data items and saves the containers so they can be reused later for other data items.
Top

The ItemContainerGenerator class maintains associations between items controls and their item containers, such as ListBox and ListBoxItem. If a control has an associated ItemContainerGenerator, you will be able to retrieve it through a property on the control

You can use the ItemContainerGenerator to retrieve items based on their index or containers by specifying the data item. For example, if you have a data-bound TreeView, and you want to get a TreeViewItem based on its index, you can use the ContainerFromIndex method. If you want to retrieve the data item, use ItemFromContainer method.

The following example shows how to retrieve an ItemContainerGenerator for a TreeView object and use the ContainerFromIndex method.

Run this sample


public MainPage()
{
    InitializeComponent();
    string[] myItems = new string[]{"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
    myTreeView.DataContext = myItems;
}
static int count = 1;
private void Button_Click(object sender, RoutedEventArgs e)
{
    TreeViewItem item = (TreeViewItem)myTreeView.ItemContainerGenerator.ContainerFromIndex(3);
    item.IsExpanded = true;
    if (count < 5)
    {
        item.Items.Add("Child " + count.ToString());
        count++;
    }
}



<StackPanel x:Name="LayoutRoot" Background="White">
    <sdk:TreeView x:Name="myTreeView" Width="200" ItemsSource="{Binding}" Margin="5"/>
    <Button Content="Add Child to Item 4" Width="150" Click="Button_Click"/>
</StackPanel>


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

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

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)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.