简体   繁体   English

在wpf中按行选择列表框内的多个复选框

[英]select multiple checkbox inside listbox by row clicking in wpf

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: 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>

If your listbox items only contain checkboxes, you can force those CheckBox es to occupy the full item's width: 如果您的列表框项目仅包含复选框,则可以强制这些CheckBox占据整个项目的宽度:

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

If your listbox items also contain some other elements, things get more complicated. 如果您的列表框项目还包含其他一些元素,则事情将变得更加复杂。

You can eg bind the IsSelected property of each ListBoxItem to your data item's IsSelected property: 您可以例如将每个ListBoxItemIsSelected属性绑定到数据项的IsSelected属性:

<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>

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM