Version

Overview of Item Calculator

ItemCalculator

The ItemCalculator component allows you to perform calculations on data bound to the control and exposes a collection of ItemCalculations, which each identify a property on a specified item that can be the source and / or target of a formula. In addition to performing the calculations, the component also exposes the results of the calculations.

The xamCalculationManager™ control can perform calculation using the ItemCalculator values by accessing the ReferenceId properties.

ItemCalculator control properties chart

The table below explains the properties of the ItemCalculator and lists their default settings.

Property Type Description Default Value

Collection of calculations that will be performed on the item.

null

Object

Specifies the item whose properties will provide the source values for the calculations.

null

Returns a dictionary that contains the results of the item calculations.

null

Specifies the CalculationManager that is used to perform the calculations.

null

String

List of properties of an item to exclude from calculations being performed on them.

Null

String

List of properties of an item to allow calculations to be performed on them.

null

String

Specifies the id that identifies the calculator to the calculation network.

null

IValueConverter

Specifies the converter used to convert the value of the source item to the value that is used in the calculation network.

null

ItemCalculation sub-control properties chart

The table below explains the properties of the ItemCalculation and lists their default settings.

Property Type Description Default Value

String

This property is optional. If this property is specified the result of the calculation is set on this property.

null

Type

Specifies the target type values of the object. For example if the TargetProperty is of type String but contains string representations of date values, then this property should be set to DateTime.

null

String

If TreatAsType property is set, this property is ignored. Specifies what type to treat the values of the object. For example if the TargetType is of type String but contains string representations of date values, then this property should be set to “datetime”.

null

Type

This property is read-only. Specifies what type to treat the values of the object.

null

String

Specifies the formula used to calculate a value for the item.

null

String

Specifies the control id that identifies the item calculation to the calculation network.

null

Code Example: Using Item Calculator

Example description

The following code snippet uses the ItemCalculator to bind the shipping detail. Formulas are then defined using the ItemCalculation component. The results of the formulas are accessed in two ways:

The first TextBlock binds the Text property of the ItemCalculator’s Results dictionary.

The xamCalculationManager accesses the ItemCalculator’s ReferenceId and performs calculations using the ReferenceId.

In XAML:

<Grid x:Name="LayoutRoot">
   <Grid.Resources>
      <ig:XamCalculationManager x:Key="CalcManager">
      </ig:XamCalculationManager>
   </Grid.Resources>
   <ig:ItemCalculatorElement x:Name="itemCalcElement"
                             CalculationManager="{StaticResource CalcManager}"
                  Item="{Binding Path=CurrentShippingDetail}">
      <ig:ItemCalculatorElement.Calculator>
         <ig:ItemCalculator ReferenceId="ShippingDetailCalculator">
            <ig:ItemCalculator.Calculations>
               <ig:ItemCalculation TargetProperty="ShipCharge"
                             Formula="([Price] * [Quantity])"/>
               <ig:ItemCalculation ReferenceId="Total"
                             Formula="([Price] * [Quantity]) + [ShipCharge]"/>
            </ig:ItemCalculator.Calculations>
         </ig:ItemCalculator>
      </ig:ItemCalculatorElement.Calculator>
   </ig:ItemCalculatorElement>
   <!-- Note that the following 2 TextBlocks will have the Text properties
        set to the same value.
     The first TextBlock binds its Text property to the calculator's Results
        dictionary.
     The second one takes advantage of the ItemCalculator's ReferenceId (which is
        optional) to pick up the Total by setting a Formula-->
   <TextBlock x:Name="Result0" Text="Total From Results”/>
   <TextBlock x:Name="Result1"
              Text="{Binding ElementName=itemCalcElement,
                     Path=Calculator.Results[Total].Value}"/>
   <TextBlock Text="TotalFromReferenceID"/>
   <TextBlock x:Name="Result2"
              ig:XamCalculationManager.CalculationManager="{StaticResource CalcManager}">
      <ig:XamCalculationManager.ControlSettings>
         <ig:ControlCalculationSettings Formula="[ShippingDetailCalculator/Total]" />
      </ig:XamCalculationManager.ControlSettings>
   </TextBlock>
</Grid>