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

前端 未结 1 982
说谎
说谎 2021-01-02 20:05

This is the ListBox code:



        
相关标签:
1条回答
  • 2021-01-02 20:22

    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 讨论(0)
提交回复
热议问题