New to Telerik UI for .NET MAUI? Start a free 30-day trial

.NET MAUI DataGrid Delegate Aggregate Descriptor

The DelegateAggregateDescriptor allows you to define property lookup logic and a custom function that are applied over the property values of the .NET MAUI DataGrid, which accumulates an aggregated result based on the component data.

To set up the DelegateAggregateDescriptor, use the following properties:

  • ValueLookup—Defines an IKeyLookup instance, which retrieves the value from the underlying ViewModel that is used for computing the aggregated value.
  • Function—Defines an IAggregateFunction instance that performs the aggregation of the values as specified by the ValueLookup property.
  • Format—Defines the string format that will be applied over the aggregated value.

The following example uses the DelegateAggregateDescriptor and a custom implementation for a SumIf function which sums the values in a range that meet a certain criteria:

1. First, create a class that inherits from the IKeyLookup interface. It will return the values of a Price property declared in our business model that is of type double.

public class SumIfKeyLookUp : IKeyLookup
{
    public object GetKey(object instance) => ((Data)instance).Price; 
}

2. Then, declare a class that inherits from the IAggregateFunction interface. This class will contain our logic for the SumIf function which we will later implement through XAML:

public class SumIfAggregateFunction : IAggregateFunction
{
    private double value;
    public double GreaterThanValue { get; set; }

    public object GetValue() => $"SumIf (Price > {this.GreaterThanValue}): " + string.Format("{0:C}", this.value);

    public IAggregateFunction Clone() => new SumIfAggregateFunction() { GreaterThanValue = this.GreaterThanValue };

    public void Accumulate(object value)
    {
        var price = (double)value;
        if (price > this.GreaterThanValue)
        {
            this.value += price;
        }
    }

    public void Merge(IAggregateFunction aggregateFunction)
    {
        var myFunction = aggregateFunction as SumIfAggregateFunction;
        if (myFunction != null)
        {
            this.value += myFunction.value;
        }
    }
}

3. Declare the DelegateAggregateDescriptor in XAML.

<telerik:RadDataGrid x:Name="dataGrid"
                     UserGroupMode="Disabled"
                     AutoGenerateColumns="False"
                     UserEditMode="Cell"
                     ShowColumnFooters="True">
    <telerik:RadDataGrid.Columns>
        <telerik:DataGridTextColumn PropertyName="Name" />
        <telerik:DataGridNumericalColumn PropertyName="Price"
                                         CellContentFormat="{}{0:C}">
            <telerik:DataGridNumericalColumn.AggregateDescriptors>
                <telerik:DelegateAggregateDescriptor>
                    <telerik:DelegateAggregateDescriptor.ValueLookup>
                        <local:SumIfKeyLookUp/>
                    </telerik:DelegateAggregateDescriptor.ValueLookup>
                    <telerik:DelegateAggregateDescriptor.Function>
                        <local:SumIfAggregateFunction GreaterThanValue="100"/>
                    </telerik:DelegateAggregateDescriptor.Function>
                </telerik:DelegateAggregateDescriptor>
            </telerik:DataGridNumericalColumn.AggregateDescriptors>
        </telerik:DataGridNumericalColumn>
    </telerik:RadDataGrid.Columns>
</telerik:RadDataGrid>

The following image shows the end result.

Delegate Aggregate Descriptor

See Also

In this article