views:

451

answers:

1

Hi, I would like a checkbox in the header of my datagrid, however, I can't seem to center align the checkbox.

This is my xaml:

<data:DataGrid 
x:Name="myDataGrid"
VerticalAlignment="Top" 
Width="300" 
Grid.Column="0" 
AutoGenerateColumns="False">
<data:DataGrid.Columns>                    
    <data:DataGridCheckBoxColumn Binding="{Binding IsNew}" Width="80">
        <data:DataGridCheckBoxColumn.HeaderStyle>
            <Style TargetType="dataprimitives:DataGridColumnHeader">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Border Background="Red" Width="80" HorizontalAlignment="Center">
                                <CheckBox
                                    x:Name="chkAll" 
                                    HorizontalAlignment="Center" 
                                    HorizontalContentAlignment="Center"
                                    Click="chk_Click">
                                </CheckBox>
                            </Border>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </data:DataGridCheckBoxColumn.HeaderStyle>
    </data:DataGridCheckBoxColumn>
    <data:DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" Width="50" />
    <data:DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" Width="100" />
</data:DataGrid.Columns>

A screenshot of my datagrid can be seen at: http://img686.imageshack.us/img686/5109/datagrid.png

I only used the border to illustrate the problem, but I've also tried a Grid and Stackpanel as well as placing the checkbox directly under the DataTemplate tag, all to no avail. How can I get the checkbox to center?

Any help will be greatly appreciated.

+1  A: 

The problem is the default control Template for DataGridColumnHeader has a 12 pixel provision on the right for the appearance of the SortIcon. Remove your Border and add Margin="12,0,0,0" to the CheckBox and it should line up.

AnthonyWJones
Sweet! Thanks, that worked a treat :)
Adnan