Show / Hide Table of Contents

Практическое руководство. Получение элемента ListBoxItem

Если вам нужно получить определенный ListBoxItem с определенного индекса в ListBox, можно использовать 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>

Приведенный ниже показано, как способ получения элемента путем указания индекс элемента в ContainerFromIndex свойство ItemContainerGenerator.

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()) + ".";
Back to top Неофициальная документация по .NET на русском языке. Лицензия: CC-BY 4.0. Основано на документации по .NET с Microsoft Docs
Generated by DocFX