New to Telerik UI for WinUI? Download free 30-day trial

DelegateAggregateDescriptor

The DelegateAggregateDescriptor allows you to define a property look up logic and a custom function that is applied over the property values. This accumulates an aggregated result based on the RadDataGrid's data.

To setup the descriptor use the following properties:

  • ValueLookup: Defines an IKeyLookup instance used to retrieve the value from the underlying view model used for aggregated value computation.
  • 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.

To display the aggregates, set the AggregatesTemplate property of the RadDataGrid's column that should display the value. The data context in the template is a collection with AggregateResult objects. Each AggregateResult contains the aggregated value and the value with the Format applied.

The following example shows how to implement IKeyLookup and IAggregateFunction and then use them with the DelegateAggregateDescriptor.

First, create a class that inherits from the IKeyLookup interface. It will return the values of a Value property that is of type double.

public class CustomIKeyLookUp : IKeyLookup
{   
    public object GetKey(object instance)
    {
        var data = instance as MyDataModel;
        return data.Value;
    }   
}

Then, implement a class that inherits from the IAggregateFunction interface. This function will return the difference between the maximum and minimum values of the Values property.

public class MyAggregateFunction : IAggregateFunction
{
    private double min = double.MaxValue;
    private double max;

    public object GetValue()
    {
        return this.max - this.min;
    }

    public void Accumulate(object value)
    {
        double val = Convert.ToDouble(value, CultureInfo.InvariantCulture);
        this.min = Math.Min(this.min, val);
        this.max = Math.Max(this.max, val);
    }


    public void Merge(IAggregateFunction aggregateFunction)
    {
        var myFunction = aggregateFunction as MyAggregateFunction;
        if (myFunction != null)
        {
            this.min = Math.Min(this.min, myFunction.min);
            this.max = Math.Max(this.max, myFunction.max);
        }
    }

    public IAggregateFunction Clone()
    {
        return new MyAggregateFunction();
    }
}

Create a DelegateAggregateDescriptor and add it to the grid's AggregateDescriptors collection.

<telerikGrid:RadDataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
    <telerikGrid:RadDataGrid.Columns>
        <telerikGrid:DataGridTextColumn PropertyName="Value">
            <telerikGrid:DataGridTextColumn.AggregatesTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <TextBlock Text="Min/Max Delta:" />
                        <TextBlock Text="{Binding [0].Value}" Margin="5 0 0 0" />
                    </StackPanel>
                </DataTemplate>
            </telerikGrid:DataGridTextColumn.AggregatesTemplate>
        </telerikGrid:DataGridTextColumn>
    </telerikGrid:RadDataGrid.Columns>
    <telerikGrid:RadDataGrid.AggregateDescriptors>
        <telerikData:DelegateAggregateDescriptor>
            <telerikData:DelegateAggregateDescriptor.ValueLookup>
                <local:CustomIKeyLookUp />
            </telerikData:DelegateAggregateDescriptor.ValueLookup>
            <telerikData:DelegateAggregateDescriptor.Function>
                <local:MyAggregateFunction />
            </telerikData:DelegateAggregateDescriptor.Function>
        </telerikData:DelegateAggregateDescriptor>
    </telerikGrid:RadDataGrid.AggregateDescriptors>
</telerikGrid:RadDataGrid>

This example uses the following namespaces:

xmlns:telerikGrid="using:Telerik.UI.Xaml.Controls.Grid"

You can get the aggregate value/s through the GetAggregateValue and GetAggregateValues methods of theIDataView interface.

In this article
Not finding the help you need?