Associate CheckBox with DataTemplate with TemplatedParent in ListBox

I have the following code:

  <ListBox Grid.Column="1" Grid.Row="4" Grid.RowSpan="2" Margin="0,0,1,0" MinHeight="80" Name="lbThemes" SelectionMode="Multiple" IsEnabled="True">                         
<ListBox.ItemTemplate>
   <DataTemplate>
      <StackPanel>
        <CheckBox x:Name="ThemeCheckbox" />
        <TextBlock Text="{Binding Path=label, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
       </StackPanel>
    </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

I want to bind my checkbox in dataTemplate to the ListBoxItem IsSelected property. Any idea how I can do this? PS I use multiple selection mode

+3
source share
1 answer

Try to execute

<CheckBox x:Name="ThemeCheckbox" IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" />
+4
source

Source: https://habr.com/ru/post/1768192/


All Articles