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?!!
Mostly like argument values in converter can't be parsed to double. (At time of load, passed values to converter will be empty string which can't be parsed to double. Hence error).
Use TryParse
to see if value can be converted to double or not. If not return empty string else return sum of both double values.
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;
}