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. 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. 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>

4. Define a sample data:

public class Data : NotifyPropertyChangedBase
{
    private string name;
    private double price;
    private double deliveryPrice;
    private int quantity;

    public string Name
    {
        get => this.name;
        set
        {
            if (value != this.name)
            {
                this.name = value;
                this.OnPropertyChanged();
            }
        }
    }

    public double Price
    {
        get => this.price;
        set
        {
            if (value != this.price)
            {
                this.price = value;
                this.OnPropertyChanged();
            }
        }
    }

    public double DeliveryPrice
    {
        get => this.deliveryPrice;
        set
        {
            if (value != this.deliveryPrice)
            {
                this.deliveryPrice = value;
                this.OnPropertyChanged();
            }
        }
    }

    public int Quantity
    {
        get => this.quantity;
        set
        {
            if (value != this.quantity)
            {
                this.quantity = value;
                this.OnPropertyChanged();
            }
        }
    }
}

5. Set a source to the RadDataGrid.ItemsSource proeprty:

this.dataGrid.ItemsSource = new List<Data>
{
    new Data { Name = "KeyBoard", Price = 24.6, DeliveryPrice = 2, Quantity = 32 },
    new Data { Name = "Mouse", Price = 30.9, DeliveryPrice = 2, Quantity = 54 },
    new Data { Name = "Video Card", Price = 760.7, DeliveryPrice = 3, Quantity = 17 },
    new Data { Name = "Motherboard", Price = 210.4, DeliveryPrice = 4, Quantity = 12 },
    new Data { Name = "SSD", Price = 42.9, DeliveryPrice = 3, Quantity = 88 },
    new Data { Name = "RAM", Price = 50, DeliveryPrice = 4, Quantity = 126 }
};

The following image shows the end result.

Delegate Aggregate Descriptor

For the DataGrid DelegateAggregateDescriptor example refer to the SDKBrowser Demo application and navigate to the DataGrid > Aggregates category.

In this article