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 anIKeyLookupinstance, which retrieves the value from the underlyingViewModelthat is used for computing the aggregated value. -
Function—Defines anIAggregateFunctioninstance that performs the aggregation of the values as specified by theValueLookupproperty. -
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 interface.
-
First, create a class that inherits from the
IKeyLookupinterface. It will return the values of aValueproperty that is of typedouble.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
IAggregateFunctioninterface. This function will return the difference between the maximum and minimum values of theValuesproperty.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
DelegateAggregateDescriptorand add it to theAggregateDescriptorscollection of the DataGrid.The previous example uses the following namespaces:<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>DataGrid with a Defined DelegateAggregateDescriptorxmlns:telerikGrid="using:Telerik.UI.Xaml.Controls.Grid"
