1

我有一个看起来像这样的 ListBox:

<ListBox>
    <ListBoxItem>Item1</ListBoxItem>
    <ListBoxItem>Item2</ListBoxItem>
    <ListBoxItem>Item3</ListBoxItem>
    <ListBoxItem>Item4</ListBoxItem>
</ListBox>

有没有办法查看所选项目的数组?我计划根据 ListBoxItem[Number?] 执行特定操作。

4

2 回答 2

4

我认为您正在寻找SelectedItem房产,或者可能是SelectedIndex房产和Items房产。或者也许是SelectedItems财产。你的问题不是很清楚你想要什么。

于 2012-05-29T00:31:39.693 回答
0

您可以使用以下示例使用数组语法访问列表框的项目,但请注意,还有更多更容易解决的方法。我也更喜欢上面现有的答案。

在下面添加以下命名空间:

using System.Windows.Controls.Primitives;

然后尝试编写以下代码。

EX
        ListBox x = new ListBox();
        ListBoxItem[] y = x.GetVisualDescendants().OfType<ListBoxItem>().ToArray();
        y[0].Content = "Foo";
于 2012-05-29T00:50:35.827 回答