开发者

How to get selected items from listbox has checkboxes in WPF?

This is the ListBox code:

<ListBox x:Name="courseslistview" 
         ItemsSou开发者_开发问答rce="{Binding .}" 
         FontSize="18.667" 
         FontFamily="Trebuchet MS" 
         LayoutUpdated="courseslistview_LayoutUpdated">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding .}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

How can I use C# to get all the checked checkboxes in the above ListBox?


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>

Then you can get the checked/selected items from the ListBox.SelectedItems collection. You'd also have to set SelectionMode to Multiple.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜