In WPF I'm using custom ColumnHeaderStyle ...
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Border Background="#3ec9ed"
BorderBrush="Black"
BorderThickness="0"
Padding="10"
CornerRadius="25">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="5,0,5,0" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground"
Value="White" />
<Setter Property="HorizontalContentAlignment"
Value="Center" />
<Setter Property="Padding"
Value="15" />
<Setter Property="SeparatorBrush"
Value="Red" />
<Setter Property="SeparatorVisibility"
Value="Visible" />
</Style>
</DataGrid.ColumnHeaderStyle>
Now it looks like this.
I want to add a little space between every column header. Like this
What can I do to achieve this?


The
DataGridwraps all column headers into a common top levelDataGridColumnHeadercontainer (it's basically a big single column that contains the individual column headers).When you set the
DataGridColumnHeader.Backgroundvia the globalStylethe value also applies to this outer container which will in effect make the gap between the column headers disappear (because they share the sameBackground.The solution is to set the
Backgroundfor this top level column to e.g.Brushes.Transparent. The top level column usually has aDataGridColumnHeader.DisplayIndexof-1.This allows us to address this special column using a template
Trigger.You must also wire your template elements to the templated parent properly in order to allow the
DataGridColumnHeaderproperties likeBackgroundorMarginto behave as expected (i.e. to have any effect on the layout). You usually use theTemplateBindingmarkup extension for this.To enable styling of the first and last item/column border individually (to apply the round corners), you need a
MultiBindingwith a customIMultiValueConverter. The purpose of this converter is to detect the last item/column.The
ItemIndexComparerConverterfrom the below example allows to specify the index of the item of interest using the Index notation (for example^1to reference the last item or1to reference the second item). Simply pass the index value of the desired column to theMultiBinding.ConverterParameterproperty.The fixed and improved
Stylethat applies a gap between the columns of 10 DIP (Marginleft and right of 5 DIP) could look as follows:ItemIndexComparerConverter.cs
Converter that returns
truewhen theIndexreference value of theMultiBinding.ConverterParameterequals the item's index, otherwisefalse.Required input (via
MultiBinding) is the current item's index and theItemsControlof the related items source collection.