我有一个看起来像这样的 ListBox:
<ListBox>
<ListBoxItem>Item1</ListBoxItem>
<ListBoxItem>Item2</ListBoxItem>
<ListBoxItem>Item3</ListBoxItem>
<ListBoxItem>Item4</ListBoxItem>
</ListBox>
有没有办法查看所选项目的数组?我计划根据 ListBoxItem[Number?] 执行特定操作。
我认为您正在寻找SelectedItem
房产,或者可能是SelectedIndex
房产和Items
房产。或者也许是SelectedItems
财产。你的问题不是很清楚你想要什么。
您可以使用以下示例使用数组语法访问列表框的项目,但请注意,还有更多更容易解决的方法。我也更喜欢上面现有的答案。
在下面添加以下命名空间:
using System.Windows.Controls.Primitives;
然后尝试编写以下代码。
EX
ListBox x = new ListBox();
ListBoxItem[] y = x.GetVisualDescendants().OfType<ListBoxItem>().ToArray();
y[0].Content = "Foo";