Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind CheckBox from DataTemplate to TemplatedParent in ListBox

Tags:

c#

wpf

I have next 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 can I do this? P.S. I have use Multiple Selesction mode

like image 697
Polaris Avatar asked Jan 27 '26 06:01

Polaris


1 Answers

Try the following

<CheckBox x:Name="ThemeCheckbox" IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" />
like image 74
rudigrobler Avatar answered Jan 29 '26 19:01

rudigrobler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!