How to set the horizontal alignment on summary columns in WPF DataGrid?
The appearance of Summary cells (Table summary, Group summary and Caption summary) is customized by writing styles for those cells.
WPF DataGrid (SfDataGrid) exposes the properties TableSummaryCellStyle, GroupSummaryCellStyle, CaptionSummaryCellStyle that customizes the appearance of the cell. Also, DataGrid exposes the following properties to customize the appearance of summary cells conditionall
Property | Description |
TableSummaryCellStyleSelector | Used to customize the alignment of Table Summary cell |
GroupSummaryCellStyleSelector | Used to customize the alignment of Group Summary cell. |
CaptionSummaryCellStyleSelector | Used to customize the alignment of Caption Summary cell. |
Now you can learn how to change the horizontal alignment of summary cells for a particular column only using StyleSelector properties exposed in DataGrid. Create a class deriving from StyleSelector and override SelectStyle method (When WinRT overrides SelectStyleCore method) and return styles based on condition. In the SelectStyle override method, you can access the Cell (for eg: GridTableSummaryCell) and from cell you can get the column details and customize the alignment based on column names. Refer the following code example.
public class TableSummaryStyleSelector : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
var cell = container as GridTableSummaryCell;
// TableSummaryStyle1 and TableSummaryStyle2 are the styles defined in Xaml.
if (cell.ColumnBase.GridColumn.MappingName == "QS1" || cell.ColumnBase.GridColumn.MappingName == "QS2")
return App.Current.Resources["TableSummaryStyle1"] as Style;
return App.Current.Resources["TableSummaryStyle2"] as Style;
}
}
The following TableSummaryStyle1 and TableSummaryStyle2 styles are returned to the Table Summary Cells based on the column name. Refer the following code example.
<Window.Resources>
<Style x:Key="TableSummaryStyle1" TargetType="syncfusion:GridTableSummaryCell">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="BorderBrush" Value="#FF7fd0de" />
</Style>
<Style x:Key="TableSummaryStyle2" TargetType="syncfusion:GridTableSummaryCell">
<Setter Property="HorizontalContentAlignment" Value="Right"/>
<Setter Property="BorderBrush" Value="#FF7fd0de" />
</Style>
<local:TableSummaryStyleSelector x:Key="tableSummaryStyleSelector" />
</Window.Resources>
The following TableSummaryCellStyleSelector property is used to customize the horizontal alignment of table summary cells. Refer the following code example.
<syncfusion:SfDataGrid x:Name="sfDataGrid"
AutoGenerateColumns="False"
AllowFiltering="True"
ColumnSizer="Star"
ItemsSource="{Binding SalesDetails}"
NavigationMode="Row"
TableSummaryCellStyleSelector="{StaticResource tableSummaryStyleSelector}"
GroupCaptionTextFormat="Sales details in {ColumnName} : {Key}">

