다음을 통해 공유


방법: ListBoxItem 가져오기

업데이트: 2007년 11월

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 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
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()) + ".";
}

목록 상자 항목을 검색한 뒤에는 다음 예제에서처럼 항목의 내용을 표시할 수 있습니다.

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()) + ".";