c# - 如何从列表框中获取选定的项目在 WPF 中有复选框?

标签 c# wpf checkbox 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>

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

最佳答案

最好将 CheckBox 绑定(bind)到 ListBoxItemIsSelected 属性,如下所示:

<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/

相关文章:

c# - 无法将类型 ApplicationUser 隐式转换为 User

c# - ObservableCollection 不更新 ListView

wpf - 捕获未处理的异常后应用程序仍然崩溃

c# - 我如何在 Glyphs 中实现字符连字?

javascript - 如果文本输入中的条件在 javascript 中为 true,则停用复选框

c# - 如何在 C# 的 TreeView 中显示多个复选框?

javascript - 使用 <td> 而不是 <tr> 显示/隐藏行

c# - 自动滚动到 WinRT 应用程序中的 ListView 底部

c# - 在 C# 中格式化 Int 时如何强制符号

c# - 奇怪的 tcpclient https 发布响应