如何:获取 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>
以下示例演示如何通过在 ItemContainerGenerator 的 ContainerFromIndex 属性中指定项的索引来检索该项。
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()) + "."
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈