Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ItemContainerGenerator.ContainerFromIndex Method

Returns the container for the item at the specified index within the ItemCollection.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

public DependencyObject ContainerFromIndex(
	int index
)

Parameters

index
Type: System.Int32
The index of the item to retrieve.

Return Value

Type: System.Windows.DependencyObject
The container for the item at the specified index within the item collection, if the item has a container; otherwise, null.

If there is no item at the specified index or the item has no container, the ContainerFromIndex method returns null.

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.

Community Additions

Show:
© 2017 Microsoft