This is a migrated thread and some comments may be shown as answers.

Binding String type to MaskedNumericInput

1 Answer 81 Views
MaskedInput (Numeric, DateTime, Text, Currency)
This is a migrated thread and some comments may be shown as answers.
Kotte
Top achievements
Rank 1
Kotte asked on 16 Sep 2011, 04:11 PM
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.

1 Answer, 1 is accepted

Sort by
0
Alex Fidanov
Telerik team
answered on 21 Sep 2011, 09:17 AM
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 >>

Tags
MaskedInput (Numeric, DateTime, Text, Currency)
Asked by
Kotte
Top achievements
Rank 1
Answers by
Alex Fidanov
Telerik team
Share this question
or