Articles in this section
Category / Section

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

2 mins read

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?

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