wpfcheckboxcenteringstretching

Center checkbox in UniformGrid's cells in WPF


I want to center and stretch checkbox in UniformGrid's cell.
I've tried to do it in many ways:

<UniformGrid>
        <CheckBox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                  HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
        <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" />
        <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" ClipToBounds="True" />
        <CheckBox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ClipToBounds="True" />

but it doesn't satisfy me because I want to stretch (just like in cell (0,0)) and center (just like in (1,0)) those checkboxes in their cells simultaneously.

How can I do it?


[edit 1]

I'm trying to make it so that I don't have to click the little check but instead can click the entire cell.

[edit 2]

Actually I want to add those checkboxes from C# code so if solution made in XAML is not 'portable' to C# language it will be not very helpful.


Solution

  • If I use this simple markup with the default Stretch values:

    <UniformGrid>
        <CheckBox Content="This is a checkbox" />
        <CheckBox Content="This is a checkbox" />
        <CheckBox Content="This is a checkbox" />
        <CheckBox Content="This is a checkbox" />
    </UniformGrid>
    

    I am able to click anywhere in each cell of the UniformGrid to check and uncheck the Checkbox in that cell.

    Functionally this is doing what you want. if you want to change the appearance of the checkbox, you can use a ControlTemplate.

    If you don't want to change the size of the CheckBox and you just want to center it visually while being able to click the cell to check it, you could use a template like this:

    <CheckBox>
        <CheckBox.Template>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <Grid Background="Transparent">
                    <CheckBox IsChecked="{TemplateBinding Property=IsChecked}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </CheckBox.Template>
    </CheckBox>
    

    EDIT:

    To apply it as a resource at runtime, write the XAML in a resource section of the appropriate scope. You can place it in the local Resources dictionary if you only need it in one control, or if you want access to it globally, put it in the App.xaml like so:

    <Application.Resources>
        <ControlTemplate x:Key="StretchedCheckBox" TargetType="{x:Type CheckBox}">
            <Grid Background="Transparent">
                <CheckBox IsChecked="{TemplateBinding Property=IsChecked}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
        </ControlTemplate>
    </Application.Resources>
    

    Notice how it has a key "StretchedCheckBox". Now you can use that to apply it in code:

    ControlTemplate stretchedTemplate = FindResource("StretchedCheckBox") as ControlTemplate;
    
    CheckBox chkBox = new CheckBox();
    chkBox.Template = stretchedTemplate;