Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate a value in WPF Binding

Tags:

c#

binding

wpf

xaml

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".

The first part is easy, and looks like 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 a nice easy way to get the product in there as well?

Using Pavlo Glazkov's solution, I modified it to look like 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 the all-important

<Window.Resources>
    <local:MultiplyFormulaStringConverter x:Key="MultiplyFormulaStringConverter"/>
</Window.Resources>

Thanks!

like image 810
coldandtired Avatar asked Feb 20 '11 14:02

coldandtired


People also ask

How does data binding work in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

What is one way binding in WPF?

In One Way binding, source control updates the target control, which means if you change the value of the source control, it will update the value of the target control. So, in our example, if we change the value of the slider control, it will update the textbox value, as shown below.

What is element binding in WPF?

Data binding in Windows Presentation Foundation (WPF) provides a simple and consistent way for apps to present and interact with data. Elements can be bound to data from different kinds of data sources in the form of . NET objects and XML.

How many types of binding are there in WPF?

WPF binding offers four types of Binding. Remember, Binding runs on UI thread unless otherwise you specify it to run otherwise. OneWay: The target property will listen to the source property being changed and will update itself.


2 Answers

Instead of using StringFormat create a converter. Something like 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>
like image 178
Pavlo Glazkov Avatar answered Oct 11 '22 19:10

Pavlo Glazkov


You could use a converter and pass as a parameter the two values that you would like to calculate. The converter would do the calculation and then return the string result.

(Converter example here)

like image 24
Madeleine Avatar answered Oct 11 '22 18:10

Madeleine