Welcome to Xceed DataGrid for Silverlight > Manipulating Data > Statistical Functions |
Statistical functions are used to calculate various statistical results from the values of one (or more) columns. In order to use a statistical function, it must first be added to the StatFunctions collection of either the grid or group configuration (see DataGridGroupConfiguration class), which defines the scope of the values that will be used by the function.
Each function that is added to the StatFunctions collection must provide a unique, identifying name through its ResultPropertyName property, from which the result of the statistical function will be retrieved. The name of the column whose values will be used to calculate the result must correspond to the FieldName property of one of the columns in a grid and is provided by the SourcePropertyName property. When required (e.g., Covariance function), multiple source property names can be provided by separating the names with commas (",").
Statistical functions defined at the grid level can only be displayed at the grid level or outside the grid. Likewise, statistical functions defined in a group configuration are only available to the groups to which the configuration is applied. |
Available statistical functions
The results of statistical functions can be displayed within a StatRow, which is a specialized row designed to display the results in alignment with the columns whose values were used to calculate the results. To display the result of a statistical function in a StatRow, the ResultPropertyName property of a cell (see StatCell class) must be set to the ResultPropertyName of the desired statistical function (see example below). By default, a StatCell has a default StatResultConverter assigned to its ResultConverter property allowing formatting information to be provided directly to the StatCell through the use of the ResultConverterParameter and ResultConverterCulture properties.
In addition to displaying statistical results in a StatRow, the results can be displayed within any element located inside or outside a grid. There are 3 common scenarios when displaying statistical results in an element other than a StatRow:
Regardless of the chosen scenario, the element must be bound to the StatContext attached property, which is defined by the DataGridControl class. Unlike the GroupContext attached property, which exposes its various properties directly, the StatContext exposes its dynamically created properties through an indexer. For example:
<TextBlock Text="{Binding ElementName=parentStackPanel, Path=(sldg:DataGridControl.StatContext)[GROUP_PERSONCOUNT].Value, StringFormat='{}{0} people in total'}"/>
The StatContext attached property is set on the first (i.e., root) element in the layout that defines how the result of a statistical functions are displayed. The root element can access the StatContext through a RelativeSource binding using the Self mode (see example below) while any child elements can access the parent's StatContext through an ElementName binding (see examples above and below).
Only the results of the a grid's statistical functions can be displayed outside of a grid. If a new DataTemplate is provided for a StatCell's content template (see ContentTemplate property), the parent StatRow must have a name and an ElementName binding must be used to bind the content of the StatCell to the StatContext (see example below). |
A StatResultConverter can be used to format the result of a statistical function, as well as display a user-friendly message when an exception occurs within the function during the calculation of its result. To format the result of a statistical function, any composite format string (see Composite Formatting topic in Windows SDK) can be used.
Composite format strings use braces ("{}") to identify place-holders, which causes an issue when providing a format string to a StatResultConverter in XAML; therefore, the StatResultConverter supports square brackets ("[]") as a substitute for braces. For example, if normally {0:#0.0} would have been used, it would be replaced with [#0.0] when using a StatResultConverter. If braces are preferred, the format string must be preceded with a closing brace (e.g., ConverterParameter={}{0:#0.0}}).
When using square brackets, the 0: that represents the place-holder must be omitted otherwise an exception will be thrown. |
Various error messages can be displayed when an exception occurs during the calculation of a statistical functions result, some of which can be modified. The following table provides a list of the default messages that can be returned by a StatResultConverter, and links to the properties that allow the messages for the specific exception to be modified:
Exception | Default message |
---|---|
DivideByZeroMessage | #DIV/0# |
Exception | #[Exception.Message]# |
InvalidSourcePropertyNameException | #[InvalidSourcePropertyNameException.SourcePropertyName]# |
InvalidValueMessage | #VALUE# |
OverflowMessage | #OVER# |
NotSupportedStatFunctionMessage | #NOTSUPPORTED# |
<Grid x:Name="LayoutRoot"> <Grid.RowDefinitions> <RowDefinition Height="24" /> <RowDefinition /> </Grid.RowDefinitions> <!-- Only the statistics that are calculated at the grid level can be accessed and displayed outside the grid. --> <TextBlock Text="{Binding ElementName=sldgDataGridControl, Path=(sldg:DataGridControl.StatContext)[TOTALPERSONCOUNT].Value, StringFormat='{}{0} people in total'}" Grid.Row="0" /> <sldg:DataGridControl x:Name="sldgDataGridControl" ItemsSource="{Binding Path=People}" AutoCreateColumns="False" Grid.Row="1"> <sldg:DataGridControl.GroupDescriptions> <sldg:DataGridGroupDescription PropertyName="Age"> <sldg:DataGridGroupDescription.GroupConfiguration> <sldg:DataGridGroupConfiguration> <sldg:DataGridGroupConfiguration.StatFunctions> <!-- Any source property can be used when calculating the result of the count function. --> <sldg:CountFunction ResultPropertyName="GROUP_PERSONCOUNT" SourcePropertyName="FirstName" /> </sldg:DataGridGroupConfiguration.StatFunctions> <sldg:DataGridGroupConfiguration.FixedFooters> <DataTemplate> <sldg:StatRow x:Name="parentElement"> <sldg:StatCell FieldName="FirstName" ResultPropertyName="GROUP_PERSONCOUNT"> <sldg:StatCell.ContentTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding ElementName=parentElement, Path=(sldg:DataGridControl.GroupContext).GroupValue, StringFormat='{}The {0} age group contains '}" /> <!-- The StatContext is set on the StatRow and can be accessed from the DataTemplate by using and ElementName binding. Obviously, this implies that the parent StatRow has an explicitly defined name. Unlike the GroupContext, which exposes its various properties directly, the StatContext exposes its dynamically created properties through an indexer. --> <TextBlock Text="{Binding ElementName=parentElement, Path=(sldg:DataGridControl.StatContext)[GROUP_PERSONCOUNT].Value, StringFormat='{}{0} people'}"/> </StackPanel> </DataTemplate> </sldg:StatCell.ContentTemplate> </sldg:StatCell> </sldg:StatRow> </DataTemplate> </sldg:DataGridGroupConfiguration.FixedFooters> </sldg:DataGridGroupConfiguration> </sldg:DataGridGroupDescription.GroupConfiguration> </sldg:DataGridGroupDescription> </sldg:DataGridControl.GroupDescriptions> <sldg:DataGridControl.StatFunctions> <!-- Any source property can be used when calculating the result of the count function. --> <sldg:CountFunction ResultPropertyName="TOTALPERSONCOUNT" SourcePropertyName="FirstName" /> <sldg:MedianFunction ResultPropertyName="MEDIANAGE" SourcePropertyName="Age" /> </sldg:DataGridControl.StatFunctions> <sldg:DataGridControl.FixedFooters> <sldg:StatRow> <sldg:StatCell FieldName="FirstName" ResultPropertyName="TOTALPERSONCOUNT" ResultConverterParameter="{}{0} people in total" /> </sldg:StatRow> <TextBlock Text="{Binding RelativeSource={RelativeSource Self}, Path=(sldg:DataGridControl.StatContext)[MEDIANAGE].Value, StringFormat='The median age is {0}'}" /> </sldg:DataGridControl.FixedFooters> <sldg:DataGridControl.Columns> <sldg:Column FieldName="FirstName" Title="First Name" /> <sldg:Column FieldName="LastName" Title="Last Name" /> <sldg:Column FieldName="Age" /> <sldg:Column FieldName="Birthday"/> <sldg:Column FieldName="HasChildren" Title="Children" VisiblePosition="99"> <!-- This DataTemplate will automatically be applied as the corresponding StatCell's content template. --> <sldg:Column.CellContentTemplate> <DataTemplate> <CheckBox IsChecked="{Binding Path=HasChildren}" /> </DataTemplate> </sldg:Column.CellContentTemplate> </sldg:Column> </sldg:DataGridControl.Columns> </sldg:DataGridControl> </Grid>