Binding String type to MaskedNumericInput

2 posts, 0 answers
  1. Kotte
    Kotte avatar
    48 posts
    Member since:
    Feb 2011

    Posted 16 Sep 2011 Link to this post

    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.
  2. Alex Fidanov
    Admin
    Alex Fidanov avatar
    636 posts

    Posted 21 Sep 2011 Link to this post

    Hi Kotte,

    If you want to be able to input only positive numbers and not negative, you can set the Minimum attached property on the numeric input control:

    <telerik:RadMaskedNumericInput xmlns:ext="clr-namespace:Telerik.Windows.Controls.MaskedInput;assembly=Telerik.Windows.Controls.Input" ext:MaskedInputExtensions.Minimum="0"/>
    Please let me know if that is what you are looking for.

    Greetings,
    Alex Fidanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
Back to Top