We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Alignment = Center for multi line grid column header

Hi,

I'm able to center a column header. But when there are headers with multiple lines, you can see, that not the text is centered but the "container" (as shown in the attached screenshot).

The multiple line header was build like 
e.Column.HeaderText = $"NormalHeadline\rShortHeadline\rLongLongLongHeadline";

Regards

Harald

Attachment: GridHead1_c92ecdc4.zip

6 Replies

SV Srinivasan Vasu Syncfusion Team October 31, 2016 11:45 AM UTC

Hi Harald, 
 
Thanks for contacting Syncfusion support. 
 
We have checked your query and You can set multiple line header text alignment as center using HeaderTemplate in GridColumn. 
 
Please refer the below code example. 
 
Code Example: 
<syncfusion:GridTextColumn  
                                           MappingName="Number" > 
                    <syncfusion:GridTextColumn.HeaderTemplate> 
                        <DataTemplate> 
                            <TextBlock Text="NormalHeadline ShortHeadline LongLongLongHeadline"  
TextAlignment="Center" TextWrapping="Wrap"/> 
                        </DataTemplate> 
                    </syncfusion:GridTextColumn.HeaderTemplate> 
                </syncfusion:GridTextColumn> 
 
 
Please refer the below UG Link: 
 
 
Regards, 
Srinivasan 
 



HB Harald Betzler October 31, 2016 01:57 PM UTC

Hi Srinivasan,

thank you. But with simple TextWrapping I've no control about the content of the particular lines. Is it possible to insert CR's?

And - due to other conditions - I have to fill the headers within the AutoGeneratingColumn event. How can I use HeaderTemplate there?

Regards

Harald


SV Srinivasan Vasu Syncfusion Team November 1, 2016 11:51 AM UTC

Hi Harald, 
 
We have checked your query and we have prepared a sample as per your requirement. In this sample, we have applied HeaderTemplate for particular column when AutoGeneratingColumn is true. 
 
Code Example: 
XAML 
 
<Application.Resources> 
        <local:ViewModel x:Key="viewmodel"/> 
        <DataTemplate x:Key="datatemplate"> 
            <TextBlock Text="{Binding TextValue}" DataContext="{StaticResource viewmodel}" TextAlignment="Center" TextWrapping="Wrap"/> 
        </DataTemplate> 
    </Application.Resources> 
 
C# 
private void grid_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e) 
        { 
            if(e.Column.MappingName == "Number") 
            { 
                e.Column.HeaderTemplate = Application.Current.Resources["datatemplate"] as DataTemplate; 
            } 
        } 
 
 
Please find the sample from the below location. 
 
 
Regards, 
Srinivasan 



HB Harald Betzler November 2, 2016 03:14 PM UTC

Hi Srinivasan,

thank you for your help.

Now I have the problem to set the header text. Because I can't use binding, I have to set the header text in code behind.

I've modified the DataTemplate to:
<DataTemplate x:Key="CenteredColumnHeaderTemplate">
     <TextBlock TextAlignment="Center" TextWrapping="Wrap"/>
</DataTemplate>

Before using the DataTemplate I've set the header text with:
e.Column.HeaderText = "...";
But now this won't work.

Regards

Harald






HB Harald Betzler November 2, 2016 04:40 PM UTC

Hi Srinivasan,

I found a solution, completely handled in code behind.
FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.SetValue(TextBlock.TextProperty, headerText);
textBlock.SetValue(StyleProperty, FindResource("GridColumnHeaderTextBlockStyle"as Style);
e.Column.HeaderTemplate = new DataTemplate(typeof(TextBlock)) {VisualTree = textBlock};
As you suggested, I create a DataTemplate containing a TextBlock with the needed properties. And then assign the DataTemplate to the HeaderTemplate property of my column.

Thank you.

Regards

Harald






SV Srinivasan Vasu Syncfusion Team November 3, 2016 04:52 AM UTC

Hi Harald, 
 
We are glad that you have fixed your issue. Please let us know if you need further assistance. 
 
Regards, 
Srinivasan 


Loader.
Live Chat Icon For mobile
Up arrow icon