如何从列表框中获取所选项目在 WPF 中有复选框? [英] How to get selected items from listbox has checkboxes in WPF?
本文介绍了如何从列表框中获取所选项目在 WPF 中有复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是列表框代码:
<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 中所有选中的复选框?
How can I use C# to get all the checked checkboxes in the above ListBox?
推荐答案
最好将 CheckBox
绑定到 ListBoxItem 的
,像这样:IsSelected
属性
It would probably be best to bind the CheckBox
to the IsSelected
property of the ListBoxItem
, like so:
<DataTemplate>
<CheckBox Content="{Binding .}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" />
</DataTemplate>
然后您可以从 ListBox.SelectedItems 集合中获取选中/选中的项目.您还必须设置 SelectionMode 到多个.
Then you can get the checked/selected items from the ListBox.SelectedItems collection. You'd also have to set SelectionMode to Multiple.
这篇关于如何从列表框中获取所选项目在 WPF 中有复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文