In my previous article, Data Binding with Value Converters, I discussed using Value Converters (implementing IValueConverter) to assist in converting values for data binding. While a simple Value Converter is good for things like converting degrees to radians, you need something slightly more complex to allow things like currency exchange.
Currency Exchange Basics:
Currency exchanges are actually pretty simple. You have an amount of money in currency A. You want to know how much that is worth in currency B. The first thing you do, is get the currency exchange rate, from a bank, or some other source (I use http://www.xe.com/). Multiply your current value (in Currency A) by this rate, and you arrive at an equivalent value of Currency B.
For example, I have $24.00 (US) in my pocket. Well, I'm currently in Iraq (Iraqi Dinars). The conversion rate from United States Dollars (USD) to Iraqi Dinars (IQD) is 1238.50. That means, 24 USD is equal to 29,724.00 Iraqi Dinars.
Implementing the Interface:
Implementing the interface is exactly like we did with Degrees and Radians, with one small change. This time, we utilize the parameter named 'parameter.' This parameter allows us to pass a value along to the IValueConverter. In this case, we will pass the exchange rate. See my sample code below. Please note that although the comments are talking about USD to IQD, this could, in theory, be used for ANY currency.
1public class Converter : IValueConverter
2{
3 //Convert from USD to IQD using the exchange rate in the parameter
4 //Exchange rate is USD -> IQD.
5 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
6 {
7 if ((string)value == "") return 0.0;
8 double dollars = System.Convert.ToDouble(value);
9 double rate = System.Convert.ToDouble(parameter);
10 double dinar = dollars * rate;
11 return dinar;
12 }
13
14 //Convert from IQD to USD using the exchange rate in the parameter.
15 //Exchange rate is USD -> IQD.
16 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
17 {
18 if ((string)value == "") return 0.0;
19 double dinar = System.Convert.ToDouble(value);
20 double rate = System.Convert.ToDouble(parameter);
21 rate = 1 / rate; //Account for the reverse conversion.
22 double dollars = dinar * rate;
23 return dollars;
24 }
25}
26
Data Binding with Parameters:
This part is easy. The framework is simply the same stuff you have been doing. I'm not going to go through the basics on Data binding with a Value Converter, you can see my other post for help doing that. The only thing we need to do, is add a parameter to the Binding statement. That parameter is named 'ConverterParameter' (How fitting). The below code demonstrates. Note, that the parameter we pass is a static value, making this good only for USD <-> IQD, however, this value can be obtained from anywhere (databound from another text box, database, file, internet).
<TextBox x:Name="txtTo"
Text="{Binding ElementName=txtFrom,
Path=Text, ConverterParameter=1238.50,
Converter={StaticResource Converter}}" />
That's it! Its not that hard at all. Now, if you actually want to implement a currency converter, you would want to obtain rates from somewhere other than this hard coded value... But you see how it works. Pretty easy!
0 comments:
Post a Comment