开发者

wpf checkbox selected when clicked outside of label

It seems my checkbox width is stretching to the extents of grid cell in which it is contained. So if you check way to the right of the checkbox label it still toggles the value. Is there a way to make the check开发者_高级运维box only toggle when the label or checkbox is clicked without hardcoding a width value for the checkbox?


If you set your column definition width to "Auto" then the column will be resized to fit the CheckBox.

However, that might ruin your layout.

An alternative is to wrap the CheckBox in a StackPanel.

    <Grid Margin="10,10,10,10" Name="grid1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="Hello"/>
        <StackPanel Grid.Column="1" Orientation="Horizontal">
            <CheckBox Content="Click Me"/>
        </StackPanel>
        <Button Grid.Column="2" Content="Press Me"/>
    </Grid>

As you can see from the first screen shot, the bounding box of the CheckBox is now just around the check and label as opposed to being the full width of the column as shown by the second.

Correct behaviour:

wpf checkbox selected when clicked outside of label


Incorrect behaviour:

wpf checkbox selected when clicked outside of label


The default HorizontalAlignment of CheckBox is Stretch. Try setting it to Left/Right/Center.


It may really stretch that wide. Try to apply some background to the CheckBox - this will show you how wide it is in reality. Also play with HorizontalAlignment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜