Custom Checkbox Datagrid not center #17884
-
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 18 replies
-
Use DevTools F12.to check is CheckBox has a MinWidth or Margin set |
Beta Was this translation helpful? Give feedback.
-
Hi @timunie , MinWidth = 0 and Margin is 0,0,0,0, any help ? |
Beta Was this translation helpful? Give feedback.
-
Ah wait the header has a reserved space for SortIcon. You need to override this. Looking up the Theme for DataGridColumnHeader should help you. |
Beta Was this translation helpful? Give feedback.
-
What control themes are you using for your DataGridColumnHeader? The defaults from Avalonia's Fluent? That one does not seem completely compatible with centered header content. But with a little tweak it could be, like this: <ControlTheme
x:Key="GridHeaderCompatibleWithCenteredContent"
BasedOn="{StaticResource {x:Type DataGridColumnHeader}}"
TargetType="DataGridColumnHeader">
<Setter Property="Padding" Value="12,0,12,0" />
<Setter Property="Template">
<ControlTemplate>
<Border
x:Name="HeaderBorder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}">
<Grid Name="PART_ColumnHeaderRoot" ColumnDefinitions="*,Auto">
<Panel HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter
x:Name="PART_ContentPresenter"
Margin="{TemplateBinding Padding}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}" />
<Path
Name="SortIcon"
Grid.Column="1"
Height="12"
MinWidth="20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Fill="{TemplateBinding Foreground}"
IsVisible="False"
Stretch="Uniform" />
</Grid>
</Panel>
<Rectangle
Name="VerticalSeparator"
Grid.Column="1"
Width="1"
VerticalAlignment="Stretch"
Fill="{TemplateBinding SeparatorBrush}"
IsVisible="{TemplateBinding AreSeparatorsVisible}" />
<Grid
x:Name="FocusVisual"
IsHitTestVisible="False"
IsVisible="False">
<Rectangle
x:Name="FocusVisualPrimary"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Fill="Transparent"
IsHitTestVisible="False"
Stroke="{DynamicResource DataGridCellFocusVisualPrimaryBrush}"
StrokeThickness="2" />
<Rectangle
x:Name="FocusVisualSecondary"
Margin="2"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Fill="Transparent"
IsHitTestVisible="False"
Stroke="{DynamicResource DataGridCellFocusVisualSecondaryBrush}"
StrokeThickness="1" />
</Grid>
</Grid>
</Border>
</ControlTemplate>
</Setter>
</ControlTheme> And use it in you datagrid: <DataGrid
ColumnHeaderTheme="{StaticResource GridHeaderCompatibleWithCenteredContent}" /> |
Beta Was this translation helpful? Give feedback.
found it: you set padding="0" to the checkbox in the header, but no padding (so it has its default padding) to the checkbox in the row cell. You should give both the same padding.