Wpf flag selected by clicking on the shortcut

It seems my checkbox width extends to the extents of the grid cell in which it is contained. Therefore, if you check the path to the right of the flag label, it still switches the value. Is there a way to check the box only when I click the label or check box without hard-coding the width value for that check box?

+6
checkbox wpf
source share
3 answers

If you set the width of the column definition to Auto, the column will be resized for the size of the CheckBox .

However, this can ruin your layout.

An alternative is the CheckBox wrapper in the 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 screenshot, the CheckBox bounding box is now next to the label and label, rather than the full column width, as shown in the second.

The correct behavior:
correct behavior
Invalid behavior:
incorrect behavior

+4
source share

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

+4
source share

It can stretch a lot. Try to apply some background to CheckBox - this will show you how real it is. Also play with HorizontalAlignment.

+3
source share

All Articles