Hi,
I'd like to use XamCalculationManager to perform calculations between several objects in my view model. The formulas will include several objects, some lists of objects and I'd like the results to be set in other object's properties.
What is your recommended way to use XamCalculationManager for this scenario ?
Thanks,
Alex
Hi Alex,
Here is a code snippet of using the ListCalculator and indices:
public class TestVM { public CellValue Result { get; set; } public List<CellValue> CellValueList { get; set; } XamCalculationManager cm = new XamCalculationManager(); ItemCalculator icResult; ListCalculator lcCellValueList; public TestVM() { Result = new CellValue(); CellValueList = new List<CellValue> { new CellValue { Value = 1m }, new CellValue { Value = 2m }, new CellValue { Value = 3m } }; lcCellValueList = new ListCalculator(); lcCellValueList.CalculationManager = cm; lcCellValueList.ItemsSource = CellValueList; lcCellValueList.ReferenceId = "ListCellValues"; icResult = new ItemCalculator(); icResult.CalculationManager = cm; icResult.Item = Result; ItemCalculation calcResult = new ItemCalculation(); calcResult.Formula = "[ListCellValues/Value(0)] + [ListCellValues/Value(2)]"; calcResult.TargetProperty = "Value"; calcResult.TreatAsType = typeof(decimal); icResult.Calculations.Add(calcResult); cm.CalculationsCompleted += new EventHandler<EventArgs>(cm_CalculationsCompleted); } void cm_CalculationsCompleted(object sender, EventArgs e) { CalculationResult result = icResult.Results.FirstOrDefault().Value; //put a breakpoint above to check the result variable } }
Diyan
Hi Diyan,
Thanks for your answer ! It works perfectly and this was what I needed.
One more question:
I saw you are using '/' instead of '.' as property accessor. What if I'd like to access indexed properties or collection items ? What syntax should I use ?
Here is a sample code that sums the Values of A and B:
public class TestVM { public CellValue A { get; set; } public CellValue B { get; set; } public CellValue Result { get; set; } XamCalculationManager cm = new XamCalculationManager(); ItemCalculator icA; ItemCalculator icB; ItemCalculator icResult; public TestVM() { A = new CellValue { Value = 1m }; B = new CellValue { Value = 2m }; Result = new CellValue(); icA = new ItemCalculator(); icA.CalculationManager = cm; icA.Item = A; icA.ReferenceId = "refA"; icB = new ItemCalculator(); icB.CalculationManager = cm; icB.Item = B; icB.ReferenceId = "refB"; icResult = new ItemCalculator(); icResult.CalculationManager = cm; icResult.Item = Result; ItemCalculation calcResult = new ItemCalculation(); calcResult.Formula = "[refA/Value] + [refB/Value]"; calcResult.TargetProperty = "Value"; calcResult.TreatAsType = typeof(decimal); icResult.Calculations.Add(calcResult); cm.CalculationsCompleted += new EventHandler<EventArgs>(cm_CalculationsCompleted); } void cm_CalculationsCompleted(object sender, EventArgs e) { CalculationResult result = icResult.Results.FirstOrDefault().Value; //put a breakpoint above to check the result variable } }
Hope this helps!
Diyan Dimitrov
The many commented lines are some things that I tried, but with no success.... the result kept telling me: "#REF!"
Hi Dyan,
I've read the help pages, but they didn't help me solve my specific issue, like how to write the formula in my case or how to set-up the references.
I'd like to compute "A.Value + B.Value" and store the result automatically in Result.Value.
Here is my code:
public class CellValue : INotifyPropertyChanged { private decimal _value;
public decimal Value
{ get { return _value; } set { _value = value;
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Value")); } } }
public event PropertyChangedEventHandler PropertyChanged; }
public class TestVM { public CellValue A { get; set; } public CellValue B { get; set; }
public CellValue Result { get; set; }
XamCalculationManager cm = new XamCalculationManager();
ItemCalculator icResult;
public TestVM() { A = new CellValue(); B = new CellValue(); Result = new CellValue();
A.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(obj_PropertyChanged); B.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(obj_PropertyChanged); Result.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(obj_PropertyChanged);
cm.CalculationsCompleted += new EventHandler<EventArgs>(cm_CalculationsCompleted);
//ItemCalculator icA = new ItemCalculator(); //icA.CalculationManager = cm; //icA.Item = A; ////icA.PropertiesToInclude = "Value"; //icA.ReferenceId = "A";
//ItemCalculation calcA = new ItemCalculation(); //calcA.ReferenceId = "AValue"; //calcA.Formula = "[A.Value] + [B.Value]"; //calcA.TreatAsType = typeof(decimal); //icA.Calculations.Add(calcA);
//ItemCalculator icB = new ItemCalculator(); //icB.CalculationManager = cm; //icB.Item = B; ////icB.PropertiesToInclude = "Value"; //icB.ReferenceId = "B";
//ItemCalculation calcB = new ItemCalculation(); //calcB.ReferenceId = "BValue"; //calcB.Formula = "[Value]"; //calcB.TreatAsType = typeof(decimal); //icB.Calculations.Add(calcB);
icResult = new ItemCalculator(); icResult.CalculationManager = cm; icResult.Item = this; icResult.ReferenceId = "Result";
ItemCalculation calcResult = new ItemCalculation(); calcResult.Formula = "[A.Value] + [B.Value]"; calcResult.ReferenceId = "CResult"; //calcResult.TargetProperty = "Value"; calcResult.TreatAsType = typeof(decimal); icResult.Calculations.Add(calcResult);
}
void cm_CalculationsCompleted(object sender, EventArgs e) { CalculationResult result = icResult.Results.FirstOrDefault().Value;
//put a breakpoint above to check the result variable }