How to bind ListCheckBox to WPF? The situation is such that the width of the checkbox is equal to the length of the text in it (less than the ListBox, and accordingly, in order for the Checked to work, you need to hover over the text and click (made crutches increased the width of the checkbox to the width of the listbox). But I would like to bind them instead of using a crutch.

<ListBox x:Name="listbox_products" Width="200" Height="300" Background="DarkGray" Margin="5,5,5,5" ItemsSource="{Binding GetAvailProductsOnProject}" > <ListBox.ItemTemplate> <DataTemplate> <CheckBox x:Name="CheckBox_CheckProduks" Width="300" IsChecked="{Binding IsSelected}" Content="{Binding Продукт}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 
  • If any of the answers helped you, tick it off (it is on the left under the buttons for voting) - Andrey NOP

2 answers 2

There are several problems here. First, you need to stretch the cell to the full width. Secondly, you need to ensure that the content of your checkbox does not go beyond the right border.

To do this, do this:

 <ListBox x:Name="listbox_products" Width="200" Height="300" Background="DarkGray" Margin="5,5,5,5" ItemsSource="{Binding GetAvailProductsOnProject}" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <ListBox.ItemTemplate> <DataTemplate> <CheckBox IsChecked="{Binding IsSelected}"> <TextBlock Text="{Binding Продукт}" TextWrapping="Wrap" /> </CheckBox> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

simple anima named Natasha

  • And how is it possible to do outside the box list (below it is admissible), a checkbox which, when checked into a boxed box, would do all the checked values ​​and vice versa not checked. - LaNC1LoT
  • @ LaNC1LoT: This is a new question, ask it separately. In two lines, the answer does not work. - VladD

All items placed in the ListBox are wrapped in a ListBoxItem, so you need to stretch ListBoxItem. You can do it like this:

 <ListBox ...> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> <Setter Property="VerticalContentAlignment" Value="Stretch"/> </Style> </ListBox.ItemContainerStyle> 

The width of the CheckBox should not be explicitly set; remove the Width setting from the markup. The CheckBox will stretch the entire width of the container (ListBoxItem) and, accordingly, the entire width of the ListBox.