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

How to overcome the DisplayBinding converter is not firing problem when XamlCompilation attribute is set as XamlCompilationOptions.Compile?

Platform: Xamarin.Forms |
Control: SfDataGrid

When the GridColumn.DisplayBinding is set in the XAML, provided XamlCompilation attribute is set as XamlCompilationOptions.Compile, the binding converter will not get fired, since the DisplayBinding property is of type “Binding” instead of “BindingBase”. Changing the type of the property would require lot of internal changes and type conversions. Thus, to resolve this issue, it is suggested to set the DisplayBinding of the column in the code behind.

Refer the below code example in which the above problem is bypassed by setting the GridColumn.DisplayBinding in code behind.

<sfGrid:SfDataGrid x:Name="dataGrid"
                   AutoGenerateColumns="False"
                   ColumnSizer="Star"
                   ItemsSource="{Binding OrdersInfo}">
  
    <sfGrid:SfDataGrid.Columns>
        <sfGrid:GridTextColumn MappingName="OrderID" />
        <sfGrid:GridTextColumn MappingName="CustomerID" />
        <sfGrid:GridTextColumn MappingName="Freight" />
        <sfGrid:GridTextColumn MappingName="Country" />
    </sfGrid:SfDataGrid.Columns>
 
  <behavior:Interaction.Behaviors>
    <behavior:BehaviorCollection>
      <behavior:EventToCommand Command="{Binding GridLoaded}"
                               CommandParameter="{x:Reference Name=dataGrid}"
                               EventName="GridLoaded" />
        </behavior:BehaviorCollection>
  </behavior:Interaction.Behaviors>
  
</sfGrid:SfDataGrid>

 

Note that the “GridColumn.DisplayBinding” is set in the ViewModel using “Behaviors” to retain the MVVM structure.

#region Fields
 
private OrderInfoRepository repository;
private Command gridLoaded;
 
#endregion
 
#region Property
 
public Command GridLoaded
{
    get
    {
        return gridLoaded;
    }
    set
    {
        gridLoaded = value;
    }
}
 
#endregion
 
#region Constructor
 
public ViewModel()
{
    repository = new OrderInfoRepository();
    gridLoaded = new Command(OnGridLoaded);
    SetRowstoGenerate(50);
}
 
#endregion
 
#region Command Implementation
 
private void OnGridLoaded(object obj)
{
    SfDataGrid sfGrid = obj as SfDataGrid;
    sfGrid.Columns[0].DisplayBinding = new Binding("OrderID", BindingMode.OneWay, new CustomConverter ());
}
 
#endregion
 

 

The following code example illustrates the implementation of the CustomConverter class.

public class CustomConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var data = int.Parse(value.ToString());
        return data + 10000;
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

 

On executing the above code example, the output is obtained as follows:

 

C:\Users\suhasini.suresh\AppData\Local\Microsoft\Windows\INetCacheContent.Word\Screenshot_2017-03-03-14-10-25_DataGridDemo.Droid.png

 

Sample Link:

How to overcome the DisplayBinding converter is not firing problem when XamlCompilation attribute is set as XamlCompilationOptions.Compile?

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

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

Live Chat Icon For mobile