我对如何从wpf中的列表框中检索多个选定值感到困惑。
在XAML中,我有以下列表框,选择模式为多个。
<ListBox Height="100" HorizontalAlignment="Left" Margin="139,207,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" SelectionChanged="listBox1_SelectionChanged" SelectionMode="Multiple" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="319,220,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
我现在如何检查foreach循环?
foreach (ListItem li in listBox1.Items)
{
?? // how to check li is selected or not
}
答案 0 :(得分:8)
您可以在ListBox.SelectedItems找到它们。
foreach (var item in listBox1.SelectedItems)
{
}
答案 1 :(得分:0)
另一个例子
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) )