-1

I'm using ImultiValueConverter and i want to convert two double values from two textboxes to double and multiply them and show the result in the third textbox this is the code in the .cs file

public  class NE_charp_converter:IMultiValueConverter
{
     public object Convert(object[] values, 
                           Type targetType,
                           object parameter,
                           System.Globalization.CultureInfo culture)
     {
         double RE_sharp = double.Parse ((string)values[0]) +
                           double.Parse((string)values[1]) ;
         return RE_sharp.ToString();
     }

     public object[] ConvertBack(object value, Type[] targetTypes, 
                                 object parameter, 
                                 System.Globalization.CultureInfo culture)
     {
         return null;
     }
}

and this is the xaml code here:

<TextBox x:Name="NE_charp_txt" Height="77" Margin="0,151,37,0" 
         VerticalAlignment="Top" HorizontalAlignment="Right" 
         Width="51.615" BorderBrush="Black">
    <TextBox.Text>
        <MultiBinding Converter="{StaticResource NE_CONVERTER}" Mode="OneWay">
            <Binding ElementName="WBC_txt" Path="Text"/>
            <Binding ElementName="NE_percent_txt" Path="Text"/>
        </MultiBinding>
    </TextBox.Text>
</TextBox>

BUT I got this message: make sure your method arguments are in the right format

what is the right form to convert the object to double and return this value?!!

4

1 回答 1

2

大多数情况下,转换器中的参数值无法解析为 double(在加载时,传递给转换器的值将是空字符串,无法解析为双精度。因此出错)

用于TryParse查看 value 是否可以转换为 double 或不。如果不返回空字符串,则返回两个双精度值的总和。

public object Convert(object[] values, Type targetType, object parameter, 
                      System.Globalization.CultureInfo culture)
{
    double firstValue = 0.0;
    double secondValue = 0.0;

    if (double.TryParse(values[0].ToString(), out firstValue) && 
        double.TryParse(values[1].ToString(), out secondValue))
    {
        double RE_sharp = firstValue + secondValue;
        return RE_sharp.ToString();
    }
    return String.Empty;
}
于 2014-08-14T17:41:31.273 回答