Windows Dev Center

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.

UIElementCollection.IndexOf Method

Updated: February 2009

Returns the index position of a specified element in a UIElementCollection.

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

public virtual int IndexOf(
	UIElement element
You cannot use methods in XAML.


Type: System.Windows.UIElement

The element whose index position is required.

Return Value

Type: System.Int32
The index position of the element. -1 if the element is not in the collection.

The following example finds the index position of a UIElement within a UIElementCollection. For the complete sample, see Finding the Index of an Element in a Panel Sample.

<DockPanel Name="ParentElement">

    <TextBlock DockPanel.Dock="Top" Name="TxtDisplay"></TextBlock>

    <Button DockPanel.Dock="Top" Click="FindIndex">What is the Index Number
        of the Element Just Added?</Button>
    <DockPanel Name="MainDisplayPanel">
        <TextBlock DockPanel.Dock="Top">Text 1</TextBlock>
        <TextBlock DockPanel.Dock="Top">Text 2</TextBlock>


		private int c_counter = 0;
		void FindIndex(object sender, RoutedEventArgs e)
			// Create a new Text element.
			TextBlock newText = new TextBlock();
			// Add this element to the UIElementCollection of the DockPanel element.
			// Add a text node under the Text element. This text is displayed. 
			newText.Text = "New element #" + c_counter;
			// Display the Index number of the new element.    
			TxtDisplay.Text = "The Index of the new element is " +  MainDisplayPanel.Children.IndexOf(newText);

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




February 2009

Indicated what the method returns if the specified element is not found.

Customer feedback.

Community Additions

© 2015 Microsoft