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.
Try to set HorizontalContentAlignment to Stretch for the ListViewItem’s
Update
Here’s a Xaml only example which centers the CheckBoxes. Paste it and try it 🙂