如何从列表框中选择项目在WPF中有复选框?

kar*_*tal 5 c# wpf checkbox listbox

这是ListBox代码:

<ListBox x:Name="courseslistview" 
         ItemsSource="{Binding .}" 
         FontSize="18.667" 
         FontFamily="Trebuchet MS" 
         LayoutUpdated="courseslistview_LayoutUpdated">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding .}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

如何使用C#获取上面ListBox中所有选中的复选框?

Cod*_*ked 18

将它绑定CheckBoxIsSelected属性可能是最好的ListBoxItem,如下所示:

<DataTemplate>
    <CheckBox Content="{Binding .}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

然后,您可以从ListBox.SelectedItems集合中获取已检查/选定的项目.您还必须将SelectionMode设置为Multiple.