c# - How to calculate a value in WPF Binding -


i have app uses 2 sliders generate product used elsewhere in code. have product value bound textblock or tooltip, example, "10 x 15 = 150".

the first part easy, , looks this:

<textblock.text>     <multibinding stringformat="{}{0} x {1}">         <binding elementname="amount_slider" path="value" />         <binding elementname="frequency_slider" path="value"/>     </multibinding> </textblock.text> 

but what's nice easy way product in there well?

using pavlo glazkov's solution, modified this:

public class multiplyformulastringconverter : imultivalueconverter {     public object convert(object[] values, type targettype, object parameter, cultureinfo culture)     {         var doublevalues = values.cast<double>().toarray();         double x = doublevalues[0];         double y = doublevalues[1];         var leftpart = x.tostring() + " x " + y.tostring();         var rightpart = (x * y).tostring();         var result = string.format("{0} = {1}", leftpart, rightpart);         return result;     }      public object[] convertback(object value, type[] targettypes, object parameter, cultureinfo culture)     {         throw new notimplementedexception();     } } 

and all-important

<window.resources>     <local:multiplyformulastringconverter x:key="multiplyformulastringconverter"/> </window.resources> 

thanks!

instead of using stringformat create converter. this:

public class multiplyformulastringconverter : imultivalueconverter {     public object convert(object[] values, type targettype, object parameter, cultureinfo culture)     {         var doublevalues = values.cast<double>().toarray();          var leftpart = string.join(" x ", doublevalues);          var rightpart = doublevalues.sum().tostring();          var result = string.format("{0} = {1}", leftpart, rightpart);          return result;     }      public object[] convertback(object value, type[] targettypes, object parameter, cultureinfo culture)     {         throw new notimplementedexception();     } } 

<textblock.text>     <multibinding converter="{staticresource multiplyformulastringconverter}">         <binding elementname="amount_slider" path="value" />         <binding elementname="frequency_slider" path="value"/>     </multibinding> </textblock.text> 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -