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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Alignment = Center for multi line grid column header

Thread ID:

Created:

Updated:

Platform:

Replies:

127148 Oct 28,2016 03:25 PM UTC Nov 3,2016 04:52 AM UTC WPF 6
loading
Tags: SfDataGrid
Harald Betzler
Asked On October 28, 2016 03:25 PM UTC

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\r\nShortHeadline\r\nLongLongLongHeadline";

Regards

Harald

Attachment: GridHead1_c92ecdc4.zip

Srinivasan Vasu [Syncfusion]
Replied On 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 
 


Harald Betzler
Replied On 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

Srinivasan Vasu [Syncfusion]
Replied On 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 


Harald Betzler
Replied On 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





Harald Betzler
Replied On 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





Srinivasan Vasu [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;