Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering a checkbox in a GridViewColumn?

I'm currently struggling on getting my CheckBoxes to properly center within my GridViewColumns.

I've defined a Style for my CheckBoxes like so:

<Style TargetType="{x:Type CheckBox}" x:Key="DataGridCheckBox">
    <Setter Property="HorizontalAlignment" Value="Center" />
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="IsEnabled" Value="False" />
    <Setter Property="Margin" Value="4" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type GridViewColumn}},Path=ActualWidth}" />
</Style>

And my CheckBoxes are added into the GridViewColumn using a DataTemplate like this:

<GridViewColumn Header="Comment">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <CheckBox Style="{StaticResource DataGridCheckBox}" IsChecked="{Binding PropertyItem.Comment, Converter={StaticResource booleanConverter}, ConverterParameter='string'}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

But the problem I have is that the CheckBoxes remain left-aligned (even when resizing the column).

Any ideas?

Thanks in advance,
Sonny

EDIT: I've been messing around with a CheckBoxin a blank window and I think the problem may be related to the CheckBoxcontrol. If I make a very wide CheckBox, I still can't seem to get the CheckBoxportion of it to align within itself. It always wants to go to the upper-left. As per the name, the ContentAlignment properties only seem to align the content.

like image 429
Sonny Boy Avatar asked Aug 31 '25 16:08

Sonny Boy


1 Answers

Try to set HorizontalContentAlignment to Stretch for the ListViewItem's

<ListView ...>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    <ListView.ItemContainerStyle>
    <!-- ... -->
</ListView>

Update

Here's a Xaml only example which centers the CheckBoxes. Paste it and try it :)

<Grid>
    <ListView>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.Resources>
            <Style TargetType="{x:Type CheckBox}" x:Key="DataGridCheckBox">
                <Setter Property="HorizontalAlignment" Value="Center" />
                <Setter Property="HorizontalContentAlignment" Value="Center" />
                <Setter Property="IsEnabled" Value="False" />
                <Setter Property="Margin" Value="4" />
                <Setter Property="VerticalAlignment" Value="Center" />
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type GridViewColumn}},Path=ActualWidth}" />
            </Style>
        </ListView.Resources>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Comment">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Style="{StaticResource DataGridCheckBox}" IsChecked="True"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
        <ListViewItem>Item1</ListViewItem>
        <ListViewItem>Item2</ListViewItem>
        <ListViewItem>Item3</ListViewItem>
    </ListView>
</Grid>
like image 135
Fredrik Hedblad Avatar answered Sep 03 '25 11:09

Fredrik Hedblad