# DelegateAggregateDescriptor

The `DelegateAggregateDescriptor` allows you to define property look-up logic and a custom function that are applied over the property values of the 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.

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

The following example shows how to implement the `IKeyLookup` and `IAggregateFunction` and use them with the `DelegateAggregateDescriptor`. You can get the aggregate values through the `GetAggregateValue` and `GetAggregateValues` methods of the [`IDataView`](https://www.telerik.com/winui/documentation/controls/raddatagrid/dataview) interface.

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

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

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

```C#
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();
	}
}
```

1. Create a `DelegateAggregateDescriptor` and add it to the `AggregateDescriptors` collection of the DataGrid.

```XAML
<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>
```

The previous example uses the following namespaces:

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

**DataGrid with a Defined DelegateAggregateDescriptor**

![WinUI ](https://www.telerik.com/winui/documentation/assets/64a5367178bf09442d2e34c139429284/datagrid-aggregates-delegateaggregatedescriptor-0.png)
