Skip to main content

Using Statistical Functions

The following page provides a list of examples that demonstrate how to validate data.

tip

All examples in this topic assume that the grid is bound to the OrderDetails table of the Northwind database, unless stated otherwise.

Displaying statistical functions

The following example demonstrates how to display the results of various statistical functions in and outside of a grid.

<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orderdetails"
Source="{Binding Source={x:Static Application.Current},
Path=OrderDetails}">
<xcdg:DataGridCollectionViewSource.StatFunctions>
<xcdg:CountFunction ResultPropertyName="orderid_count"
SourcePropertyName="OrderID"/>
<xcdg:SumFunction ResultPropertyName="unitprice_sum"
SourcePropertyName="UnitPrice"/>
<xcdg:SumFunction ResultPropertyName="quantity_sum" SourcePropertyName="Quantity"/>
</xcdg:DataGridCollectionViewSource.StatFunctions>
<xcdg:DataGridCollectionViewSource.GroupDescriptions>
<xcdg:DataGridGroupDescription PropertyName="ProductID"/>
</xcdg:DataGridCollectionViewSource.GroupDescriptions>
</xcdg:DataGridCollectionViewSource>
<xcdg:StatResultConverter x:Key="valueConverter"/>
</Grid.Resources>
<DockPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<TextBlock Text="Total Orders: "/>
<TextBlock Text="{Binding ElementName=OrderDetailsGrid,
Path=StatContext.orderid_count}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<TextBlock Text="Average Unit Price: "/>
<TextBlock Text="{Binding ElementName=OrderDetailsGrid,
Path=StatContext.unitprice_average,
Converter={StaticResource valueConverter},
ConverterParameter=f2}"/>
</StackPanel>
<xcdg:DataGridControl x:Name="OrderDetailsGrid"
ItemsSource="{Binding Source={StaticResource cvs_orderdetails}}"
DockPanel.Dock="Bottom">
<xcdg:DataGridControl.DefaultGroupConfiguration>
<xcdg:GroupConfiguration>
<xcdg:GroupConfiguration.Footers>
<DataTemplate>
<xcdg:StatRow>
<xcdg:StatCell FieldName="UnitPrice"
ResultPropertyName="unitprice_sum"/>
<xcdg:StatCell FieldName="Quantity"
ResultPropertyName="quantity_sum"/>
<xcdg:StatCell FieldName="OrderID"
ResultPropertyName="orderid_count"/>
</xcdg:StatRow>
</DataTemplate>
</xcdg:GroupConfiguration.Footers>
</xcdg:GroupConfiguration>
</xcdg:DataGridControl.DefaultGroupConfiguration>
</xcdg:DataGridControl>
</DockPanel>
</Grid>
Changing statistical contexts

The following example demonstrates how to change the statistical context of a TextBlock that displays the results of a statistical function when the current item is changed. The statistical context of the TextBlock will be changed in the grid's PropertyChanged event handler by using the GetParentGroupFromItem method to retrieve the current group and set it as the new statistical context.

To simplify the code below, the DataContext of the StackPanel could have been modified rather than the DataContext of each TextBlock.

The implementation of the PropertyChanged event handler is located below.

<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orderdetails"
Source="{Binding Source={x:Static Application.Current},
Path=OrderDetails}">
<xcdg:DataGridCollectionViewSource.StatFunctions>
<xcdg:CountFunction ResultPropertyName="orderid_count"
SourcePropertyName="OrderID"/>
<xcdg:SumFunction ResultPropertyName="unitprice_sum"
SourcePropertyName="UnitPrice"/>
<xcdg:SumFunction ResultPropertyName="quantity_sum" SourcePropertyName="Quantity"/>
</xcdg:DataGridCollectionViewSource.StatFunctions>
<xcdg:DataGridCollectionViewSource.GroupDescriptions>
<xcdg:DataGridGroupDescription PropertyName="ProductID"/>
</xcdg:DataGridCollectionViewSource.GroupDescriptions>
</xcdg:DataGridCollectionViewSource>
<xcdg:StatResultConverter x:Key="valueConverter"/>
</Grid.Resources>
<DockPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<TextBlock Text="Total Orders: "/>
<TextBlock Text="{Binding ElementName=OrderDetailsGrid,
Path=StatContext.orderid_count}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<TextBlock Text="Average Unit Price: "/>
<TextBlock Text="{Binding ElementName=OrderDetailsGrid,
Path=StatContext.unitprice_average,
Converter={StaticResource valueConverter},
ConverterParameter=f2}"/>
</StackPanel>
<xcdg:DataGridControl x:Name="OrderDetailsGrid"
ItemsSource="{Binding Source={StaticResource cvs_orderdetails}}"
DockPanel.Dock="Bottom">
<xcdg:DataGridControl.DefaultGroupConfiguration>
<xcdg:GroupConfiguration>
<xcdg:GroupConfiguration.Footers>
<DataTemplate>
<xcdg:StatRow>
<xcdg:StatCell FieldName="UnitPrice"
ResultPropertyName="unitprice_sum"/>
<xcdg:StatCell FieldName="Quantity"
ResultPropertyName="quantity_sum"/>
<xcdg:StatCell FieldName="OrderID"
ResultPropertyName="orderid_count"/>
</xcdg:StatRow>
</DataTemplate>
</xcdg:GroupConfiguration.Footers>
</xcdg:GroupConfiguration>
</xcdg:DataGridControl.DefaultGroupConfiguration>
</xcdg:DataGridControl>
</DockPanel>
</Grid>
Custom statistical-result layout

The following example demonstrates how to create a custom data template that will be used in the footers of the first-level groups to display the results of various statistical functions.

  <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orderdetails"
Source="{Binding Source={x:Static Application.Current},
Path=OrderDetails}">
<xcdg:DataGridCollectionViewSource.StatFunctions>
<xcdg:CountFunction ResultPropertyName="orderid_count"
SourcePropertyName="OrderID"/>
<xcdg:SumFunction ResultPropertyName="unitprice_sum"
SourcePropertyName="UnitPrice"/>
<xcdg:AverageFunction ResultPropertyName="unitprice_average"
SourcePropertyName="UnitPrice"/>
<xcdg:SumFunction ResultPropertyName="quantity_sum"
SourcePropertyName="Quantity"/>
</xcdg:DataGridCollectionViewSource.StatFunctions>
<xcdg:DataGridCollectionViewSource.GroupDescriptions>
<xcdg:DataGridGroupDescription PropertyName="ProductID"/>
</xcdg:DataGridCollectionViewSource.GroupDescriptions>
</xcdg:DataGridCollectionViewSource>
<xcdg:StatResultConverter x:Key="valueConverter"/>
</Grid.Resources>
<xcdg:DataGridControl x:Name="OrderDetailsGrid"
ItemsSource="{Binding Source={StaticResource cvs_orderdetails}}">
<xcdg:DataGridControl.DefaultGroupConfiguration>
<xcdg:GroupConfiguration>
<xcdg:GroupConfiguration.Footers>
<xcdg:GroupHeaderFooterItemTemplate VisibleWhenCollapsed="True">
<DataTemplate>
<Border Background="#999999"
BorderBrush="LightBlue"
BorderThickness="3"
Margin="5" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Total Orders: " Grid.Row="0" Grid.Column="0"/>
<TextBlock Text="{Binding RelativeSource={RelativeSource Self},
Path=(xcdg:DataGridControl.StatContext).orderid_count}"
Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="Total Quantity Sold: " Grid.Row="1" Grid.Column="0"/>
<TextBlock Text="{Binding RelativeSource={RelativeSource Self},
Path=(xcdg:DataGridControl.StatContext).quantity_sum}"
Grid.Row="1" Grid.Column="1"/>
<TextBlock Text="Total Sales: " Grid.Row="2" Grid.Column="0"/>
<TextBlock Text="{Binding RelativeSource={RelativeSource Self},
Path=(xcdg:DataGridControl.StatContext).unitprice_sum}"
Grid.Row="2" Grid.Column="1"/>
<TextBlock Text="Average Unit Price: " Grid.Row="3" Grid.Column="0"/>
<TextBlock Text="{Binding RelativeSource={RelativeSource Self},
Path=(xcdg:DataGridControl.StatContext).unitprice_average,
Converter={StaticResource valueConverter},
ConverterParameter=f2}"
Grid.Row="3" Grid.Column="1"/>
</Grid>
</Border>
</DataTemplate>
</xcdg:GroupHeaderFooterItemTemplate>
</xcdg:GroupConfiguration.Footers>
</xcdg:GroupConfiguration>
</xcdg:DataGridControl.DefaultGroupConfiguration>
<xcdg:DataGridControl.View>
<xcdg:CardView AllowCardResize="True"/>
</xcdg:DataGridControl.View>
</xcdg:DataGridControl>
</Grid>
Displaying statistical functions in details

The following example demonstrates how to explicitly define detail descriptions for the DataRelations found in the DataTable to which the grid is bound and how to calculate statistical functions for a detail description whose results will be displayed in the StatRows contained in the footer sections of the details to which the description's corresponding detail configuration will be applied.

  <Grid>
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_employees"
Source="{Binding Source={x:Static Application.Current},
Path=Employees}">
<xcdg:DataGridCollectionViewSource.DetailDescriptions>
<xcdg:DataRelationDetailDescription RelationName="Employee_Orders"
Title="Employee Orders">
<xcdg:DataRelationDetailDescription.DetailDescriptions>
<xcdg:DataRelationDetailDescription RelationName="Order_OrderDetails"
Title="Order Details">
<xcdg:DataRelationDetailDescription.ItemProperties>
<xcdg:DataGridItemProperty Name="UnitPrice" />
<xcdg:DataGridItemProperty Name="Quantity" />
<xcdg:DataGridItemProperty Name="Discount" />
</xcdg:DataRelationDetailDescription.ItemProperties>
<xcdg:DataRelationDetailDescription.StatFunctions>
<xcdg:SumFunction ResultPropertyName="sum_quantity"
SourcePropertyName="Quantity" />
<xcdg:AverageFunction ResultPropertyName="average_unitprice"
SourcePropertyName="UnitPrice" />
</xcdg:DataRelationDetailDescription.StatFunctions>
</xcdg:DataRelationDetailDescription>
</xcdg:DataRelationDetailDescription.DetailDescriptions>
</xcdg:DataRelationDetailDescription>
</xcdg:DataGridCollectionViewSource.DetailDescriptions>
</xcdg:DataGridCollectionViewSource>
</Grid.Resources>

<xcdg:DataGridControl x:Name="EmployeesGrid"
ItemsSource="{Binding Source={StaticResource cvs_employees}}"
ItemsSourceName="Employee Information"
AutoCreateDetailConfigurations="True">
<xcdg:DataGridControl.DetailConfigurations>
<xcdg:DetailConfiguration RelationName="Employee_Orders">
<xcdg:DetailConfiguration.DetailConfigurations>
<xcdg:DetailConfiguration RelationName="Order_OrderDetails">
<xcdg:DetailConfiguration.Footers>
<DataTemplate>
<xcdg:StatRow Background="AliceBlue">
<xcdg:StatCell FieldName="UnitPrice"
ResultPropertyName="average_unitprice"
ResultConverterParameter="f2" />
<xcdg:StatCell FieldName="Quantity"
ResultPropertyName="sum_quantity" />
</xcdg:StatRow>
</DataTemplate>
</xcdg:DetailConfiguration.Footers>
</xcdg:DetailConfiguration>
</xcdg:DetailConfiguration.DetailConfigurations>
</xcdg:DetailConfiguration>
</xcdg:DataGridControl.DetailConfigurations>
</xcdg:DataGridControl>
</Grid>
Custom statistical function

The following example demonstrates how to create a custom statistical function based on the CountFunction, which will only count the items if they match the specified conditions.

  <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
xmlns:local="clr-namespace:Xceed.Wpf.Documentation"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_products"
Source="{Binding Source={x:Static Application.Current}, Path=Products}">
<xcdg:DataGridCollectionViewSource.StatFunctions>
<local:CountIfFunction ResultPropertyName="CountProductsToOrder"
SourcePropertyName="ReorderLevel,UnitsOnOrder">
<!-- Only count products which have a ReorderLevel of 5, 10, 15, or 20, and
a UnitsOnOrder value of 0. -->
<local:CountIfFunction.Conditions>
<s:String>^5$|^10$|^15$|^20$</s:String>
<s:String>^0$</s:String>
</local:CountIfFunction.Conditions>
</local:CountIfFunction>
</xcdg:DataGridCollectionViewSource.StatFunctions>
<xcdg:DataGridCollectionViewSource.GroupDescriptions>
<xcdg:DataGridGroupDescription PropertyName="CategoryID" />
</xcdg:DataGridCollectionViewSource.GroupDescriptions>
</xcdg:DataGridCollectionViewSource>
</Grid.Resources>
<xcdg:DataGridControl x:Name="OrdersGrid"
ItemsSource="{Binding Source={StaticResource cvs_products}}">
<xcdg:DataGridControl.Columns>
<xcdg:Column FieldName="ReorderLevel"/>
</xcdg:DataGridControl.Columns>
<xcdg:DataGridControl.DefaultGroupConfiguration>
<xcdg:GroupConfiguration>
<xcdg:GroupConfiguration.Footers>
<DataTemplate>
<xcdg:StatRow Background="Pink">
<xcdg:StatCell FieldName="ReorderLevel"
ResultPropertyName="CountProductsToOrder" />
</xcdg:StatRow>
</DataTemplate>
</xcdg:GroupConfiguration.Footers>
</xcdg:GroupConfiguration>
</xcdg:DataGridControl.DefaultGroupConfiguration>
</xcdg:DataGridControl>
</Grid>