开发者

IMultiValueConverter + MultiBinding - Reverse Arithmatic Calculations on Data-Bound Values?

Apollogies for the weird title. I didnt know how else to put it.

What i have is 3 TextBoxes Data-Bound to some values.

What i need to do is have the value of 1 of the TextBoxes Automatically compute as a result of a calculation of the other 2 textboxes.

After some Google'ing, i found that using the IMultiValueConverter Interface should solve my problem. It does. But only 1 way.

For example:

TextBox 1 * TextBox 2 = TextBox 3

But the reverse is also true:

TextBox 3 / TextBox 2 = TextBox 1

The latter is what i'm having trouble with completing. No matter what i do, the reverse calculation wont stick.

I've Implemented 2 IMultiValueConverters, each for the 2 TextBoxes(since its 2 different calculations).

Converter 1:

Public Class SalaryConverter
Implements IMultiValueConverter

Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
    Dim salary As Double = 0
    salary = (Math.Round(values(0) * (values(1) * 4)))
    Return salary.ToString("C")
End Function

Public Function ConvertBack(ByVal value开发者_Go百科 As Object, ByVal targetTypes() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
    Return Nothing
End Function

End Class

Converter 2:

Public Class RateConverter
Implements IMultiValueConverter

Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
    Dim rate As Double = 0
    rate = ((values(0) / values(1)) / 4)
    Return rate.ToString("C")
End Function

Public Function ConvertBack(ByVal value As Object, ByVal targetTypes() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
    Return Nothing
End Function

End Class

You'll see that the Return values for the ConvertBack methods is set to Nothing. This is because whenever i return the value parameter, i get an exception saying i cant convert from string to 1-dimensional array.

What am i doing wrong?

Any help is much appreciated.

Thanks in Advance.

P.S. I am fairly new to Converters. I only started playing around with it recently.


A multi value convert works as this :

  1. the Convert method requires n input parameters and convert it into 1 result. In your case : TextBox 1 + TextBox 2 => TextBox 3
  2. the ConvertBack is the reverse : it takes 1 input parameter and convert it into n results. In your case : TextBox 3 => TextBox 1 + TextBox 2

So your ConvertBack should return an array containing values of TextBox1 and TextBox2... If you want to update only TextBox1 for exemple, you can do :

return new object[] { myTextBox1String, Binding.DoNothing };

EDIT : link to MSDN ConvertBack : http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.convertback.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜