Hello,
I try to use your xamFormulaEditor, but I don't know how to achieve what I want, because there is no sample that fits.
What I'm trying to do is:
Step 1: Edit a formula string via xamFormulaEditor, where the operands should be 1 to n double values from an array.
Step 2: Store this string to disk. (no problem)
Step 3: Later on the program should pick up the formula string and do the calulation.
The problems are
Step 1: How can I tell the editor that there are n operands? The number of available operands will vary.
Step 3: What do I have to do in order to calculate my formula with my actual operands? There is no dialog window available at this moment.
Please could someone give me a small sample as a starter?
Maybe something with these two functions:
void OpenEditor(ref string formulaString, int numberOfOperands) // opens window to edit the formula
double Calc(string formulaString, double[] operands) // calculates the formula with the given operands
Hello Michael,
I have attached a sample where you input a number into a textbox to specify the number of arguments for a specific function named MyAvg.
After that, you need to open the FormulaEditor dialog window by pressing a button, which will update the function with the correct amount of arguments. Then it will calculate the average of the given operands.
Also, here is a link to the online documentation for the FormulaEditor: http://help.infragistics.com/Doc/WPF/Current/CLR4.0/?page=xamFormulaEditor.html
Please let me know if you have any questions or concerns.
Sincerely,AndrewDeveloper Support IInfragistics Inc.www.infragistics.com/support
Hello Andrew,
thank you for your sample.
Unfortunatly this is not quit what I need. Maybe this is due to my bad english.
1. the operands are not shown in the editor. They should be available to be selected by the user and be inserted in the formula like variables. Something like [//OP1], [//OP2] and so on.
2. I want to calculate the formula after the window and the calcManager is long since destroyed. So I tried to modify your Button2_Click event (see below) and create a new calcManager. But how can I give the calcManager the operands to use with the formula?
private void Button2_Click(object sender, RoutedEventArgs e)
{
var argumentCount = int.Parse(this.numArgs.Text);
var operands = CreateSampleOperands(argumentCount);
var formula = this.formulaEditor.Formula;
XamCalculationManager xcm = new XamCalculationManager();
xcm.RegisterUserDefinedFunction(
new CustomCalculationFunction("MyAvg", args => args.Average(), argumentCount, argumentCount));
// how to give xcm the operands before calculating?
var result = xcm.CalculateFormula(formula);
MessageBox.Show("Average: " + result.GetResolvedValue().ToString());
}
I think I have found a solution for the calculation step:
private void OnCalcTest(object sender, RoutedEventArgs e)
List<double> ValueList = new List<double> { 1, 2, 3 };
double result = CalculateFormula(ValueList, "[Measurand/Value(0)] + [Measurand/Value(1)] + [Measurand/Value(2)]");
//double result = CalculateFormula(ValueList, "[Measurand(0)] + [Measurand(1)] + [Measurand(2)]");
MessageBox.Show("Result: " + result);
public double CalculateFormula(List<double> items, string formula)
ListCalculator listCalculator = new ListCalculator()
CalculationManager =new XamCalculationManager()
};
listCalculator.ReferenceId ="Measurand";
listCalculator.ListCalculations.Add(
new ListCalculation()
ReferenceId ="Result",
Formula = formula
);
listCalculator.ItemsSource = items;
listCalculator.EnsureCalculated();
CalculationValue result = listCalculator.ListResults["Result"].CalculationValue;
listCalculator.ItemsSource =null;
listCalculator.ListCalculations.Clear();
if(result.Value is double)
return (double)result.Value;
return double.NaN;
Do you think there is any problem with this?
It would be nice if I could use a formula like
double result = CalculateFormula(ValueList, "[Measurand(0)] + [Measurand(1)] + [Measurand(2)]");
or
double result = CalculateFormula(ValueList, "[Value(0)] + [Value(1)] + [Value(2)]");
Is there a way to achieve this?
I am glad you were able to work out a solution to the issue you were having.
Please let me know if you have any further questions or concerns on this matter.
Nevermind, I found a solution.
It's much easier to work with NamedReferences. So the code to calculate the formula is:
double result = CalculateFormula(ValueList, "[Measurand1] + [Measurand2] + [Measurand3]");
xcm.NamedReferences =new NamedReferenceCollection();
for(int i = 0; i < items.Count; i++)
xcm.NamedReferences.Add(new NamedReference() { ReferenceId = "Measurand"+(i+1), Value = items[i] });
CalculationValue result = xcm.CalculateFormula(formula);
And for the formula editor it's really no problem to bind NamedReferences to a dependency property of type NamedReferenceCollection, where all the operands are listed.