0
votes

I am working on Windows 8.1 Store App using XAML and C#.

I have added 2 text boxes and implemented an IValueConverter Interface.

here is the Xaml Code

<Page.Resources>
    <local:ConverterClass x:Key="C_Converter" />
    <local:EuroConverterClass x:Key="Euro_Converter" />
    <local:YenConverterClass x:Key="Yen_Converter" />
</Page.Resources>

<TextBox Name="PKR_TextBox" 
             Grid.Row="1"
             Grid.Column="2"
             Width="450" 
             Height="50"  
             FontSize="30"
             FontWeight="Bold"
             HorizontalAlignment="Left" 
             VerticalAlignment="Center" />

<TextBox Name="DOLLAR_TextBox"
             Grid.Row="2"
             Grid.Column="2"
             Text="{Binding ElementName=PKR_TextBox, Path=Text, Converter={StaticResource C_Converter}}"
             Width="450" 
             Height="50"  
             FontSize="30"
             FontWeight="Bold"
             HorizontalAlignment="Left" 
             VerticalAlignment="Center" />

Here my Converter Class Code:

class ConverterClass : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        int pkr;
        int dollar = 0;
        if (Int32.TryParse(value.ToString(), out pkr))
        {
            dollar = pkr * 0.0099;
        }
        return dollar;          
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

I an trying to convert currency at run time when user enters a value in PKR text box it should automatically update USD Textbox. But instead it is giving me an error "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)".

Please help and ignore my bad english.

1

1 Answers

0
votes

The error message is pretty clear. You have to use double values for your calculation. double is the default floating point type when you use C#.

public object Convert(
    object value, Type targetType, object parameter, string language)
{
    double pkr;
    double dollar = 0.0;
    if (double.TryParse(value.ToString(), out pkr))
    {
        dollar = pkr * 0.0099;
    }
    return dollar; 
}