Binding HeaderText using C# not Xaml

So I am required to make a grid with variable column count and with HeaderText that can change at run-time. My issue currently is that my grid will refresh properly on my click events but not my headers, even though the model behind is valued properly.
I have found a similar solution that does it in the xaml file but I need to do it in the .cs file.
https://www.syncfusion.com/kb/7815/how-to-bind-a-view-model-property-to-header-template


Is there a way to set bindings for the Column.HeaderText(string) property in the .cs file? Here is my code.

[MainPage.xaml.cs]
public partial class MainPage : 
     public SfDataGrid dataGrid;

     public async void OnButton1Clicked(object sender, EventArgs args){
          if(this.dataGrid.Columns.Count > 0) this.dataGrid.Columns[0].HeaderText = "New Column Name";
          updateGridData();
           // This does not give the behavior I'm looking for. It does not update the header text.
     }

     public MainPage(){
          InitializeComponent();
          dataGrid = new SfDataGrid();
          bool businessCondition = true;
          dataGrid.AutoGenerateColumns = false;
          dataGrid.ItemsSource = this.viewModel.GridData;
     
          GridNumericColumn firstColumn = new GridNumericColumn();
          firstColumn.MappingName = "Tag1Count";
          firstColumn.HeaderText = this.viewModel.FirstColumnName;

          GridNumericColumn secondColumn = new GridNumericColumn();
          secondColumn .MappingName = "Tag2Count";
          secondColumn .HeaderText = this.viewModel.SecondColumnName;

          if( businessCondition ) {
               dataGrid.Columns.Add(firstColumn);
          } else {
               dataGrid.Columns.Add(firstColumn);
               dataGrid.Columns.Add(secondColumn);
           }
     
          dataGrid.VerticalOptions = LayoutOptions.FillAndExpand;
          dataGrid.HorizontalOptions = LayoutOptions.FillAndExpand;
          this.gridStackLayout.VerticalOptions = LayoutOptions.FillAndExpand;
          this.gridStackLayout.HorizontalOptions = LayoutOptions.FillAndExpand;

          this.gridStackLayout.Children.Add(dataGrid);
}

[MyViewModel.cs]
public MyViewModel{
     public ObservableCollection {get;set;} = new ObservableCollection();
     public string FirstColumnName {get;set;}
     public string SecondColumnName {get;set;}
     public MyViewModel(){
          FirstColumnName = "The First Tag";
          SecondColumnName = "The Second Tag";
          InitializeMockGridData();
     }     
}

public class GridData{
     public int Tag1Count {get;set;}
     public int Tag2Count {get;set;}
}











3 Replies 1 reply marked as answer

PK Pradeep Kumar Balakrishnan Syncfusion Team February 2, 2021 10:12 AM UTC

Hi Nitish, 
Thank you for contacting Syncfusion support. 
We have checked your requirement “How to change the column Header text in C# binding in Xamarin forms” in order to achieve this requirement we have to implement INotifyPropertyChanged to viewmodel class and raise notify listener when property is changed and define DataGrid column header template and bind the text like following code snippet. 
Code Snippet: 
GridNumericColumn firstColumn = new GridNumericColumn(); 
firstColumn.SetBinding(GridNumericColumn.HeaderTextProperty, new Binding("FirstColumnName", source: this.viewModel)); 
firstColumn.MappingName = "OrderID"; 
 
firstColumn.HeaderTemplate = new DataTemplate(() => 
{ 
    var label = new Label(); 
    label.HorizontalTextAlignment = TextAlignment.Center; 
    label.HorizontalOptions = LayoutOptions.Center; 
    label.VerticalTextAlignment = TextAlignment.Center; 
    label.SetBinding(Label.TextProperty, new Binding("FirstColumnName", source: this.viewModel)); 
    return label; 
}); 
 
GridTextColumn secondColumn = new GridTextColumn(); 
secondColumn.MappingName = "CustomerID"; 
secondColumn.HeaderTemplate = new DataTemplate(() => 
{ 
    var label = new Label(); 
    label.HorizontalTextAlignment = TextAlignment.Center; 
    label.HorizontalOptions = LayoutOptions.Center; 
    label.VerticalTextAlignment = TextAlignment.Center; 
    label.SetBinding(Label.TextProperty, new Binding("SecondColumnName", source: this.viewModel)); 
    return label; 
}); 
 
dataGrid.Columns.Add(firstColumn); 
dataGrid.Columns.Add(secondColumn); 
 
We have also attached sample for your reference in the following link. 
 
Kindly let us know if you need any further assistance on this. 
 
Regards, 
Pradeep Kumar B 


Marked as answer

NC Nitish Chauhan February 2, 2021 03:04 PM UTC

Thank you! That was it


PK Pradeep Kumar Balakrishnan Syncfusion Team February 3, 2021 04:56 AM UTC

Hi Nitish, 
 
We are glad that the given solution meets your requirement. Please get in touch with us if you would require any further assistance in future. 
 
Regards, 
Pradeep Kumar B 


Loader.
Up arrow icon