Share via


방법: ListBoxItem 가져오기

ListBox의 특정 인덱스에서 특정 ListBoxItem을 가져와야 하는 경우 ItemContainerGenerator를 사용할 수 있습니다.

예제

다음 예제에서는 ListBox 및 해당 항목을 보여 줍니다.

<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>

다음 예제에서는 ItemContainerGeneratorContainerFromIndex 속성에서 항목의 인덱스를 지정하여 항목을 검색하는 방법을 보여 줍니다.

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()) + ".";
}
Private Sub GetIndex0(ByVal Sender As Object, ByVal e As RoutedEventArgs)

    Dim lbi As ListBoxItem = CType( _
        lb.ItemContainerGenerator.ContainerFromIndex(0), ListBoxItem)
    Item.Content = "The contents of the item at index 0 are: " + _
        (lbi.Content.ToString()) + "."
End Sub

목록 상자 항목을 검색한 후 다음 예제와 같이 항목의 콘텐츠를 표시할 수 있습니다.

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