【发布时间】:2011-05-01 15:53:45
【问题描述】:
在列表框项目中使用复选框,如何从列表中获取选中的复选框
<ListBox ItemsSource="{Binding NameList}" HorizontalAlignment="Left" Margin="16,68,0,12" Name="listBox1" Width="156" IsEnabled="True" SelectionMode="Multiple" Focusable="True" IsHitTestVisible="True" IsTextSearchEnabled="False" FontSize="12" Padding="5" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding Path=CNames}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我试图循环遍历列表框项中的选定项目,但它在列表框项中引发异常
private void btnSelected(object sender, RoutedEventArgs e)
{
foreach (ListBoxItem item in listBox1.Items)
{
if (item.ToString() == "true")
{
MessageBox.Show(item.Content.ToString());
}
}
}
【问题讨论】:
-
什么是异常,从哪一行抛出异常?