GetStatContext Method
See Also  Example
Xceed.Wpf.DataGrid Assembly > Xceed.Wpf.DataGrid Namespace > DataGridControl Class : GetStatContext Method

obj
The element whose statistical context to retrieve.
Gets the context from which a statistical function retrieves the values needed to calculate its result, and that exposes the results as properties that have the same names as the functions' ResultPropertyName properties. This feature is available only in the Professional Edition

Syntax

Visual Basic (Declaration) 
Public Shared Function GetStatContext( _
   ByVal obj As DependencyObject _
) As Object
C# 
public static object GetStatContext( 
   DependencyObject obj
)

Parameters

obj
The element whose statistical context to retrieve.

Return Value

An object representing the context from which a statistical function retrieves the values needed to calculate its result, and that exposes the results as properties that have the same names as the functions' ResultPropertyName properties.

Example

All examples in this topic assume that the grid is bound to the OrderDetails table of the Northwind database, unless stated otherwise.
The following example demonstrates how to display the results of various statistical functions in and outside of a grid.
XAMLCopy Code
<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> 
  <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:StatCell FieldName="UnitPrice" 
                              ResultPropertyName="unitprice_average" 
                              ResultConverterParameter="f2"/> 
             </xcdg:StatRow> 
           </DataTemplate> 
         </xcdg:GroupConfiguration.Footers> 
        </xcdg:GroupConfiguration> 
      </xcdg:DataGridControl.DefaultGroupConfiguration>     
    </xcdg:DataGridControl> 
  </DockPanel> 
</Grid>
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.
XAMLCopy Code
<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:AverageFunction ResultPropertyName="unitprice_average" 
                              SourcePropertyName="UnitPrice"/>               
      </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="Results for product "/> 
      <TextBlock x:Name="CurrentGroupTitle" Text="{Binding Name}"/> 
      <TextBlock Text=": "/> 
      <TextBlock Text="     Total Orders-"/> 
      <TextBlock x:Name="TotalOrders" Text="{Binding orderid_count}"/> 
      <TextBlock Text="     Average Unit Price-"/> 
      <TextBlock x:Name="AveragePrice" 
                 Text="{Binding unitprice_average, 
                        Converter={StaticResource valueConverter}, 
                        ConverterParameter=f2}"/> 
    </StackPanel> 
    <xcdg:DataGridControl x:Name="OrderDetailsGrid" 
                          ItemsSource="{Binding Source={StaticResource cvs_orderdetails}}" 
                          PropertyChanged="CurrentItemChanged" 
                          DockPanel.Dock="Bottom"/> 
  </DockPanel> 
</Grid>
The following code provides the implementation of the PropertyChanged event handler.
Visual BasicCopy Code
Private Sub CurrentItemChanged( ByVal sender As Object, _
                                ByVal e AsPropertyChangedEventArgs )
  If e.PropertyName = "CurrentItem" Then

    If Me.OrderDetailsGrid.CurrentItem Is Nothing Then
      Return

    Dim group As CollectionViewGroup =
              Me.OrderDetailsGrid.GetParentGroupFromItem( Me.OrderDetailsGrid.CurrentItem )

    Me.CurrentGroupTitle.DataContext = group
    Me.TotalOrders.DataContext = group
    Me.AveragePrice.DataContext = group
  End If
End Sub
The following code provides the implementation of the PropertyChanged event handler.
C#Copy Code
private void CurrentItemChanged( object sender, PropertyChangedEventArgs e )
{
 
if( e.PropertyName == "CurrentItem" )
 {

   
if( this.OrderDetailsGrid.CurrentItem == null )
     
return;

   CollectionViewGroup group =
             
this.OrderDetailsGrid.GetParentGroupFromItem( this.OrderDetailsGrid.CurrentItem );

   
this.CurrentGroupTitle.DataContext = group;
   
this.TotalOrders.DataContext = group;
   
this.AveragePrice.DataContext = group;
 }
}
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.
XAMLCopy Code
<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>

Remarks

The values used to calculate the results of a statistical function are retrieved from its statistical context. For example, if an element that displays the results of a statistical function is contained in a group footer, the element's statistical context will be the group, and the results of the statistical function will be calculated according to the values of the data items in the group. If the same element is placed in the footers of a grid, then its statistical context is provided by a grid (through the StatContext property) and all the data items will be used to calculate the results.

The same statistical function can be used by more than one element, regardless of the statistical context. For example, if the average of a column is to be displayed in both the footers of a group and a grid, only one AverageFunction needs to be added to the StatFunctions collection.

Requirements

Supported Frameworks: Microsoft .NET Framework version 3.5

See Also