Hi,
I am using MaskedNumericInput to input numeric data.(The main reason to use the control is to restrict the user entering negative values).I am applying the mask dynamically based on the user selection from the dropdown .I am using a Converter to apply the mask dynamically it is working fine.
The database field storing this value is VarChar.
So,I added one more converter to convert the String value to double. While loading from database its working fine,but,when I try to edit the value its throwing error saying.
Unable to cast object of type 'System.Double to type System.String.
Here is the code.
<telerik:RadMaskedNumericInput xmlns:ext="clr-namespace:Telerik.Windows.Controls.MaskedInput;assembly=Telerik.Windows.Controls.Input" HorizontalAlignment="Left" AcceptsReturn="False" Width="125" AutoFillNumberGroupSeparators="False" ext:MaskedInputExtensions.Minimum="0" HorizontalContentAlignment="Left" Value="{Binding OtherResult, Converter={StaticResource StringToDoubleConverter
}, Mode=TwoWay}" IsClearButtonVisible="False" IsReadOnly="False" IsValidationHintVisible="False" Mask="{Binding Converter={StaticResource MaskTypeConverter }}" Placeholder=" " SpinMode="None" TextMode="PlainText" UpdateValueEvent="PropertyChanged" />
public class MaskTypeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var item = value as MemberMetricDetail;
if (item != null)
{
return MaskType.GetMaskType(item.LabelTypeKey);
}
return "#3.2";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class StringToDoubleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double result = 0;
var item = value as string;
if (item != null && Double.TryParse(item, out result))
{
return Double.Parse(item);
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string result = null;
var item = value as string;
if (item != null)
result = item.ToString();
return result;
}
}
Can some please help me.
Thanks,
Kotte.
I am using MaskedNumericInput to input numeric data.(The main reason to use the control is to restrict the user entering negative values).I am applying the mask dynamically based on the user selection from the dropdown .I am using a Converter to apply the mask dynamically it is working fine.
The database field storing this value is VarChar.
So,I added one more converter to convert the String value to double. While loading from database its working fine,but,when I try to edit the value its throwing error saying.
Unable to cast object of type 'System.Double to type System.String.
Here is the code.
<telerik:RadMaskedNumericInput xmlns:ext="clr-namespace:Telerik.Windows.Controls.MaskedInput;assembly=Telerik.Windows.Controls.Input" HorizontalAlignment="Left" AcceptsReturn="False" Width="125" AutoFillNumberGroupSeparators="False" ext:MaskedInputExtensions.Minimum="0" HorizontalContentAlignment="Left" Value="{Binding OtherResult, Converter={StaticResource StringToDoubleConverter
}, Mode=TwoWay}" IsClearButtonVisible="False" IsReadOnly="False" IsValidationHintVisible="False" Mask="{Binding Converter={StaticResource MaskTypeConverter }}" Placeholder=" " SpinMode="None" TextMode="PlainText" UpdateValueEvent="PropertyChanged" />
public class MaskTypeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var item = value as MemberMetricDetail;
if (item != null)
{
return MaskType.GetMaskType(item.LabelTypeKey);
}
return "#3.2";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class StringToDoubleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double result = 0;
var item = value as string;
if (item != null && Double.TryParse(item, out result))
{
return Double.Parse(item);
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string result = null;
var item = value as string;
if (item != null)
result = item.ToString();
return result;
}
}
Can some please help me.
Thanks,
Kotte.