这是列表框代码:
<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>
如何使用 C# 获取上面 ListBox 中所有选中的复选框?
最佳答案
最好将 CheckBox
绑定(bind)到 ListBoxItem
的 IsSelected
属性,如下所示:
<DataTemplate>
<CheckBox Content="{Binding .}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" />
</DataTemplate>
然后您可以从 ListBox.SelectedItems 集合中获取选中/选中的项目。您还必须设置 SelectionMode到多个。
关于c# - 如何从列表框中获取选定的项目在 WPF 中有复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6525920/