【问题标题】:How to clear the margins of the corners of a checkbox in a listbox如何清除列表框中复选框的边距
【发布时间】:2022-01-03 10:05:51
【问题描述】:

enter image description here 请检查图像。

列表框中的模板中有一个复选框,我在里面设置了红色背景色。

当我点击红色复选框时工作正常。

但是当我点击图像黑色箭头时,它没有被选中。

如何清除空格并将其与复选框同步?

我尝试设置边距但无法解决。

        <ListBox x:Name="xList" Grid.Row="1" HorizontalContentAlignment="Stretch">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Vertical" IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Background="red">
                <CheckBox IsChecked="{Binding IsVisible, Mode=TwoWay}"
                          VerticalContentAlignment="Center" 
                          Content="{Binding Header}"
                          Height="28" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

谢谢你帮助我。

【问题讨论】:

    标签: wpf xaml checkbox listbox margin


    【解决方案1】:

    将此代码添加到您的ListBox:

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Padding" Value="0" />
        </Style>
    </ListBox.ItemContainerStyle>
    

    这会将ListBoxItem 容器的Padding 设置为0。结果如下所示:

    【讨论】:

    • 我在列表框中设置它并且只在数据模板中。我没有想过设置listboxitem。这有很大的不同吗?
    • 我不确定您的确切意思,但是是的,它确实有所作为。 ItemContainerStyle 属性使您能够更改包含 DataTemplate 的容器的外观。
    猜你喜欢
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    相关资源
    最近更新 更多