Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to calculate a value in WPF Binding
    primarykey
    data
    text
    <p>I have an app which uses two sliders to generate a product used elsewhere in the code. What I would like is to have the product value bound to a textblock or tooltip, for example, to look something like "10 x 15 = 150".</p> <p>The first part is easy, and looks like this:</p> <pre><code>&lt;TextBlock.Text&gt; &lt;MultiBinding StringFormat="{}{0} x {1}"&gt; &lt;Binding ElementName="amount_slider" Path="Value" /&gt; &lt;Binding ElementName="frequency_slider" Path="Value"/&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; </code></pre> <p>But what's a nice easy way to get the product in there as well?</p> <p>Using Pavlo Glazkov's solution, I modified it to look like this:</p> <pre><code>public class MultiplyFormulaStringConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var doubleValues = values.Cast&lt;double&gt;().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(); } } </code></pre> <p>And the all-important </p> <pre><code>&lt;Window.Resources&gt; &lt;local:MultiplyFormulaStringConverter x:Key="MultiplyFormulaStringConverter"/&gt; &lt;/Window.Resources&gt; </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload