1

I'm trying to select multiple checkboxes inside a listbox by clinking the row. it's working fine when click performs on checkbox content.
Xaml:

 <ListBox ItemsSource="{Binding Templates}">
    <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <CheckBox Content="{Binding Name}" 
                          Margin="0 2 0 0"
                          Style="{StaticResource BaseCheckBox}" 
                          IsChecked="{Binding IsSelected}"
                          HorizontalAlignment="Stretch"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
  </ListBox>
3
  • What exactly are you trying to achieve? This seems like a use case where a DataGrid is better suited Commented Jan 11, 2019 at 6:51
  • @MindSwipe just trying to select checkboxes by row clicking same like checkbox content clicking. Commented Jan 11, 2019 at 7:00
  • And why not use a DataTable? Commented Jan 11, 2019 at 7:33

2 Answers 2

2

If your listbox items only contain checkboxes, you can force those CheckBoxes to occupy the full item's width:

<CheckBox 
    Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}}"/>

If your listbox items also contain some other elements, things get more complicated.

You can e.g. bind the IsSelected property of each ListBoxItem to your data item's IsSelected property:

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
    </Style>
</ListBox.ItemContainerStyle>

Then, you will be able to check the checkboxes by selecting the items in the listbox. However, the checked items will also be in a selected state.

If you don't like or don't need that, you can remove the selection visual style:

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="IsSelected" Value="{Binding IsSelected}"/>

        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
        </Style.Resources>
    </Style>
</ListBox.ItemContainerStyle>
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, there is the only checkbox in the list box and the first solution work for me. Thanks
0

Solution of this problem:

  <ListBox ItemsSource="{Binding Templates}" Style="{StaticResource ListBoxStyle}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="Focusable" Value="False"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Name}" IsChecked="{Binding IsSelected}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.