Articles in this section
Category / Section

How to bind a view model property to header template ?

1 min read

SfDataGrid allows you to bind the view model property to HeaderTemplate by setting the BindingContext of the GridColumn as ViewModel.

The below code example illustrates how to bind the view model property to Header Template.

 
<sfgrid:SfDataGrid x:Name="dataGrid" 
                    ItemsSource="{Binding OrdersInfo}" 
                    AutoGenerateColumns="False"
                    ColumnSizer="Star" 
                    SelectionMode="Single">
      <sfgrid:SfDataGrid.Columns>
        <sfgrid:GridTextColumn MappingName="OrderID">
           <sfgrid:GridColumn.HeaderTemplate>
              <DataTemplate>
               <Label BindingContext="{StaticResource viewModel}" 
                            VerticalTextAlignment ="Center"
                            HorizontalTextAlignment="Center"
                      Text="{Binding Headertext}" TextColor="Blue" IsVisible="{Binding _Visibility}"/>
              </DataTemplate>
            </sfgrid:GridColumn.HeaderTemplate>
        </sfgrid:GridTextColumn>
        <sfgrid:GridTextColumn MappingName="EmployeeID"/>
        <sfgrid:GridTextColumn MappingName="FirstName"/>
        <sfgrid:GridTextColumn MappingName="ShipCity"/>
      </sfgrid:SfDataGrid.Columns>
 </sfgrid:SfDataGrid>
 

 

Refer the following screenshot that shows the outcome upon execution of the above code.

Table

Sample Link:

How to bind a view model property to header template?

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied