Version

Configuring Unbound Field

Purpose

This topic describes how to create an unbound field in the xamDataPresenter™ controls. The unbound fields are used for a variety of purposes such as displaying a calculated value or binding to a complex property.

Required background

The following topics are prerequisites to understanding this topic:

Topic Purpose

This topic describes how to add a xamDataGrid control to your page.

This section lists the topics written specifically to help you access data using xamDataGrid control.

Configuring a Field to be Unbound

Overview

You can configure any Field to be unbound by setting its BindingType property to Unbound and this way use it to display a calculated value or a complex type in the xamDataPresenter controls. For more information about the base Field properties, read the Fields topic.

Property settings

The following table maps the desired configuration to the property settings that manage it.

In order to: Use this property: And set it to:

Bind the field to a property directly defined in the data item

This is the default value.

The binding mechanism looks for a data item property name that matches the Field's Name property exactly. This approach provides better performance than using alternate bindings.

Bind the field to a property which is not directly defined in the data item

In this case you need to specify the binding in the field’s AlternateBinding property.

Note
Note

AlternateBinding leads to additional memory and performance overhead than the default name binding.

Set a Field to be unbound

This binding type is set when the cell value is set through a mechanism other than binding, e.g. through code or as the target of a formula.

Specify if the Field should auto-release binding objects in case AlternateBinding is used

This is the default value.

A Field with an AlternateBinding creates a binding object for every cell whose value is accessed and then discards it after the cell is scrolled out of view and its value isn’t required for summary calculations. This minimizes memory usage.

Specify the Field should retain binding objects after they are created in case AlternateBinding is used

Set this property to Retain when sort, group-by or any other operations that require accessing values of all the unbound cells is performed in order to prevent performance impact.

Example

The screenshot below demonstrates how Bonus and Total Salary CurrencyField are set to be unbound in the xamDataGrid control.

Configuring Unbound Field 2.png

Following is the code that implements this example.

In XAML:

<igDP:XamDataGrid x:Name="DataGrid" BindToSampleData="True"
                  InitializeRecord="DataGrid_OnInitializeRecord">
    <igDP:XamDataGrid.FieldLayouts>
        <igDP:FieldLayout>
            <igDP:FieldLayout.Fields>
                <igDP:Field Name="name" Label="Employee Name" />
                <igDP:CurrencyField Name="salary" Label="Salary" />
                <!-- Unbound Currency Fields displaying calculated values -->
                <igDP:CurrencyField Name="bonus" Label="Bonus" BindingType="Unbound" />
                <igDP:CurrencyField Name="totalSalary" Label="Total Salary" BindingType="Unbound" />
            </igDP:FieldLayout.Fields>
        </igDP:FieldLayout>
    </igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>

In C#:

private void DataGrid_OnInitializeRecord(object sender,
            Infragistics.Windows.DataPresenter.Events.InitializeRecordEventArgs e)
{
    if (e.Record is DataRecord)
    {
        //get the current records data, perform the appropriate
        //calculations and assign the values to the unbound currency fields
        var dr = (DataRecord)e.Record;
        double salary = double.Parse(dr.Cells["salary"].Value.ToString());
        double bonus = (salary * 10)/100;
        double totalSalary = salary + bonus;
        dr.Cells["bonus"].Value = bonus;
        dr.Cells["totalSalary"].Value = totalSalary;
    }
}

In Visual Basic:

Private Sub DataGrid_OnInitializeRecord(sender As Object, e As Infragistics.Windows.DataPresenter.Events.InitializeRecordEventArgs)
    If TypeOf e.Record Is DataRecord Then
        'get the current records data, perform the appropriate
        'calculations and assign the values to the unbound currency fields
        Dim dr = DirectCast(e.Record, DataRecord)
        Dim salary As Double = Double.Parse(dr.Cells("salary").Value.ToString())
        Dim bonus As Double = (salary * 10) / 100
        Dim totalSalary As Double = salary + bonus
        dr.Cells("bonus").Value = bonus
        dr.Cells("totalSalary").Value = totalSalary
    End If
End Sub

Configuring a Field Binding Using AlternateBinding

Overview

If you need to create a field which is not bound to a property directly defined in the data item you should create a field and set its BindingType property to UseAlternateBinding. Use the field’s AlternateBinding and AlternateBindingRetentionMode properties to configure the alternate binding.

Example

The screenshot below demonstrates the AlternateBinding property example with the xamDataGrid control:

Configuring Unbound Field 3.png

Following is the code that implements the example that demonstrates the usage of the AlternateBinding property to create and bind fields to data.

The Models and ViewModel code is available here: ProductData.cs

The Field AlternateBinding property can be set in XAML or in code-behind.

Setting the AlternateBinding in XAML:

In XAML:

<Grid>
    <Grid.DataContext>
        <data:ProductData />
    </Grid.DataContext>
    <igDP:XamDataGrid x:Name="DataGrid"
                      DataSource="{Binding Path=Products}"
                      FieldLayoutInitialized="DataGrid_OnFieldLayoutInitialized">
        <igDP:XamDataGrid.FieldLayoutSettings>
            <igDP:FieldLayoutSettings AutoGenerateFields=" />
        </igDP:XamDataGrid.FieldLayoutSettings>
        <igDP:XamDataGrid.FieldLayouts>
            <igDP:FieldLayout>
                <igDP:FieldLayout.Fields>
                    <igDP:TextField Name="ProductID" Label="Product ID" />
                    <igDP:TextField Name="ProductName" Label="Product Name" />
                    <igDP:Field Label="Category Name"
                        AlternateBinding="{Binding Path=Category.CategoryName}" />
                </igDP:FieldLayout.Fields>
            </igDP:FieldLayout>
        </igDP:XamDataGrid.FieldLayouts>
    </igDP:XamDataGrid>
</Grid>

Setting the AlternateBinding in code-behind:

Adding of the newly created field is done in the FieldLayoutInitialized event handler.

In C#:

private void DataGrid_OnFieldLayoutInitialized(object sender, FieldLayoutInitializedEventArgs e)
{
    var fieldDesc = new Field
    {
        Label = "Category Description",
        AlternateBinding = new Binding("Category.Description"),
    };
    e.FieldLayout.Fields.Add(fieldDesc);
}

In Visual Basic:

Private Sub DataGrid_OnFieldLayoutInitialized(sender As Object, e As FieldLayoutInitializedEventArgs)
    Dim fieldDesc = New Field() With {
        .Label = "Category Description",
        .AlternateBinding = New Binding("Category.Description")
    }
    e.FieldLayout.Fields.Add(fieldDesc)
End Sub

Related Topics

The following topics provide additional information related to this topic.

Topic Purpose

This topic describes how to define field layouts to specify which fields to display and to set properties related to those fields in the xamDataPresenter controls.

This topic describes an easy and straightforward way of configuring specific editor fields for different data types in the xamDataPresenter controls.

This topic describes how to create a field with a custom display and edit template using a TemplateField in the xamDataPresenter controls.

This topic describes the difference between automatic and manual Fields generation in the xamDataPresenter controls.

This topic describes how you can create multiple FieldLayouts for the xamDataPresenter controls.

This topic describes how to add a Field to display an image overriding the CellValuePresenter’s template in the xamDataPresenter controls.

This topic describes how to load field customizations for the xamDataPresenter controls.

This topic describes how to save field customizations for the xamDataPresenter controls.

This topic describes how to change the layout customizing the Row, Column, ColumnSpan, or RowSpan on each Field to further control the layout of the Record.

This topic describes how to change the Record orientation.

This topic describes how to change the content flow direction.

This topic explains how to configure the controls on the data presenter field editor.