Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
295
Calculation manager from code behind
posted

All of the examples I've seen show the calculation manager used with binding to controls. Is is possibly to use it without data binding?

We would like to be able to create an equation with variables what we feed in the values of, and then have the calculation manager produce the result for us. It would be nice to use the formulat editor with those variables that we feed in. Is this possible?

  • 30945
    Verified Answer
    Offline posted

    Hello,

     

    Thank you for your post. I have been looking into it and I have created a sample application for you, that demonstrates how you can use the XamCalculationManger , by using only code behind. Please let me know if you need any further assistance on the matter.

     

    Sincerely,

    Krasimir

    Developer Support Engineer

    Infragistics

    www.infragistics.com/support

    CalcManagerInCodeBehind.zip
  • 295
    posted

     

    I tried the following but I get an error: “Source item not specified” when I try to calculate.  I think I'm specifying the instance of my TestClass as the Item to be used by the ItemCalculator.  Any clues as to what I am missing?

     

     

                XamCalculationManager calcMan = new XamCalculationManager();

                calcMan.CalculationFrequency = CalculationFrequency.Manual;

                

                

                TestClass t = new TestClass()

                {

                    Value1 = 1.1,

                    Value2 = 2.2,

                    Value3 = 3.3,

                    Result = 0

                };

     

                XamCalculationManager.SetCalculationManager(t, calcMan);

                t.SetValue(FrameworkElement.NameProperty, "testData");

     

                TextBox textbox1 = new TextBox();

                textbox1.Name = "txtQuantity";

                String val = textbox1.GetValue(FrameworkElement.NameProperty) as String;

     

     

     

                ItemCalculator calculator = new ItemCalculator();

                calculator.Item = t;

                calculator.CalculationManager = calcMan;

                calculator.ReferenceId = "myCalculator";

                calculator.PropertiesToInclude = "Value1,Value2,Value3,Result";

     

     

                ItemCalculation calc = new ItemCalculation();

                calc.Formula = "[Value1] + [Value2]";

                calc.TargetProperty = "Result";

     

                calculator.Calculations.Add(calc);

     

                calcMan.PerformCalculations();

     

                if (calculator.Results["Result"].IsError)

                {

                    Console.WriteLine("Error: " + calculator.Results["Result"].ErrorText);

                }

                else

                {

                    Console.WriteLine("Success: Value is " + calculator.Results["Result"].Value);

     

                }

     

     

        public class TestClass : DependencyObject
        {
            public double Value1
            {
                get { return (double)GetValue(Value1Property); }
                set { SetValue(Value1Property, value); }
            }
     
            // Using a DependencyProperty as the backing store for Value1Property.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty Value1Property =
                DependencyProperty.Register("Value1", typeof(double), typeof(TestClass));
     
     
     
     
            public double Value2
            {
                get { return (double)GetValue(Value2Property); }
                set { SetValue(Value2Property, value); }
            }
     
            // Using a DependencyProperty as the backing store for Value2.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty Value2Property =
                DependencyProperty.Register("Value2", typeof(double), typeof(TestClass));
     
     
     
     
            public double Value3
            {
                get { return (double)GetValue(Value3Property); }
                set { SetValue(Value3Property, value); }
            }
     
            // Using a DependencyProperty as the backing store for Value3.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty Value3Property =
                DependencyProperty.Register("Value3", typeof(double), typeof(TestClass));
     
     
     
            public double Result
            {
                get { return (double)GetValue(ResultProperty); }
                set { SetValue(ResultProperty, value); }
            }
     
            // Using a DependencyProperty as the backing store for Result.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty ResultProperty =
                DependencyProperty.Register("Result", typeof(double), typeof(TestClass));
     
            
     
        }