如何在列表框wpf中获取多个选定的项目?

我对如何从wpf中的列表框中检索多个选定值感到困惑。

在XAML中,我有以下列表框,选择模式为multiple。

 

我现在如何检查foreach循环?

  foreach (ListItem li in listBox1.Items) { ?? // how to check li is selected or not } 

您将在ListBox.SelectedItems中找到它们。

 foreach (var item in listBox1.SelectedItems) { } 

另一个例子

 int j = 0; for (int i = 0; i < lbItems.Items.Count; i++) { if (lbItems.Items[i] == lbItems.SelectedItems[0]) j++; } MessageBox.Show(lbItems.Items.IndexOf(lbItems.SelectedItems[0]).ToString() + string.Format("\r\nThere are {0} occurences of this object in this list",j) )