如何从列表框中选择项目在WPF中有复选框? [英] How to get selected items from listbox has checkboxes in WPF?

查看:158
本文介绍了如何从列表框中选择项目在WPF中有复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是ListBox代码:

This is the ListBox code:

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

推荐答案

最好将复选框绑定到 ListBoxItem 属性,如下所示:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆