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

Bind the Custom HeaderTemplate name to the MappingName

I want to customize my headers to be different colors on my SfDataGrid. The trouble is, that I have to update my header names based on some other inputs on my tool.

I am able to get the HeaderTemplate to update to my correct initial settings but cannot seem to update the value easily.

What I'm hoping is that I can bind my internal label text to the MappingName, might look something like this in XAML, but I'm looking for the C# version. 

<syncfusion:SfDataGrid.Columns>
    <syncfusion:GridTextColumn MappingName="OrderID">
        <syncfusion:GridTextColumn.HeaderTemplate>
            <DataTemplate>
                <Label x:Name="OrderID" Text="{Binding MappingName}" TextColor="Black" />
            </DataTemplate>
        </syncfusion:GridTextColumn.HeaderTemplate>
    </syncfusion:GridTextColumn>
</syncfusion:SfDataGrid.Columns> Here is kinda what I'm looking for... 

  private static DataTemplate HeaderTemplate(Color color) {
            return new DataTemplate(() => {
                var label = new Label() { Text = (#WHAT GOES HERE?#), BackgroundColor = color };
                return new ViewCell { View = label };
            });
        }



1 Reply

SS Suhasini  Suresh Syncfusion Team November 13, 2018 09:58 AM UTC

Hi Mark, 
 
Thanks for contacting Syncfusion Support. 
 
We checked your query, please find the below code snippet to achieve your requirement to set the binding for the TextProperty in code behind. 
private static DataTemplate HeaderTemplate(Color color) 
{ 
    return new DataTemplate(() => { 
        var label = new Label() 
        { 
            BackgroundColor = color 
        }; 
        label.SetBinding(Label.TextProperty, "MappingName"); 
        return new ViewCell { View = label }; 
    }); 
} 
 
Regards,    
Suhasini  


Loader.
Live Chat Icon For mobile
Up arrow icon