كيفية القيام بما يلي: الحصول على 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 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()) + ".";
}
بعد استرداد list box item ، يمكنك عرض محتويات الصنف, كما هو موضح في المثال التالي.
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()) + ".";