UIElementCollection.IndexOf(UIElement) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回 UIElementCollection 中指定項目的索引位置。
public:
virtual int IndexOf(System::Windows::UIElement ^ element);
public virtual int IndexOf (System.Windows.UIElement element);
abstract member IndexOf : System.Windows.UIElement -> int
override this.IndexOf : System.Windows.UIElement -> int
Public Overridable Function IndexOf (element As UIElement) As Integer
參數
- element
- UIElement
需要其索引位置的項目。
傳回
項目的索引位置。 如果項目不在集合中則為 -1。
範例
下列範例會尋找 內的 UIElementCollection 索引位置 UIElement 。
<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>
</DockPanel>
</DockPanel>
...
private int c_counter = 0;
void FindIndex(object sender, RoutedEventArgs e)
{
c_counter+=1;
// Create a new Text element.
TextBlock newText = new TextBlock();
// Add this element to the UIElementCollection of the DockPanel element.
MainDisplayPanel.Children.Add(newText);
// Add a text node under the Text element. This text is displayed.
newText.Text = "New element #" + c_counter;
DockPanel.SetDock(newText,Dock.Top);
// Display the Index number of the new element.
TxtDisplay.Text = "The Index of the new element is " + MainDisplayPanel.Children.IndexOf(newText);
}
Dim c_counter as Integer = 0
Private Sub FindIndex(ByVal sender As Object, ByVal args As RoutedEventArgs)
Try
Dim newText As TextBlock = New TextBlock()
c_counter = c_counter + 1
' Add this element to the UIElementCollection of the DockPanel element.
MainDisplayPanel.Children.Add(newText)
' Add a text node under the Text element. This text is displayed.
newText.Text = "New element #" & CStr(c_counter)
DockPanel.SetDock(newText, Dock.Top)
' Display the Index number of the new element.
TxtDisplay.Text = "The Index of the new element is " & MainDisplayPanel.Children.IndexOf(newText)
Catch ex As System.Exception
System.Windows.MessageBox.Show(ex.Message)
End Try
End Sub