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

RadMaskedNumericInput binding issues

2 Answers 144 Views
MaskedInput (Numeric, DateTime, Text, Currency)
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 07 Mar 2012, 11:56 PM
Hello, I am trying to use the RadMaskedNumericInput control for masking text and binding on some customer data, such as phone numbers. I have found this control to be extremely buggy and unusable. 

I am using a custom mask "(###) ###-####" and it works fine. The phone numbers are stored as 10 digit strings with no separators ("0123456789" for example). When I load a customer, the value shows up correctly masked in the control (I see (012) 345-6789). 

The main problem is that the bound property on the model never gets set. So if I am entering a new customer and type in the phone number, it does not get saved. I found that this is because the Value property of the numeric input never gets changed, as the conversion from string always fails. I have tried setting ValueToTextConverter to convert between double? and string, and I found that when NumericInput attempts to convert what is typed and store it the double? Value property, it always includes the mask in the string it's trying to convert. So it tries to convert "(012) 345-6789" to a double and this of course fails. I have tried binding to the Text property instead, but this does not work either as it includes the characters from the mask.

I have TextMode set to MaskedText, please do not recommend that as the solution because it does not work. I have tried setting it in markup and via an extended class. If I set it in markup, the typed text will stay in the control, but still the values will never be set. If I set TextMode via the extended class, the typed characters will disappear when the control loses focus, though they will reappear when it regains focus. But still the databinding will not set the model's properties.

I am using release 2011.3.1116.40 and unfortunately upgrading is not possible as this is the version provided and required by my client.

Here is the code for the value converter I used with MaskedNumericInput:

[CanConvert(typeof(System.String), typeof(System.Nullable<double>))]
public class StringToNullableDouble : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string s = value as string;
        if (s == null) return null;
        double d;
        if (Double.TryParse(s, out d)) return d;
        return null;
    }
    public object ConvertBack(object value, Type targetType,
    object parameter, System.Globalization.CultureInfo culture)
    {
        double? d = value as double?;
        if (d == null || !d.HasValue) return null;
        return d.Value.ToString();
    }
}


Here is a XAML sample:

<tel:RadMaskedNumericInput
 HorizontalAlignment="Left"
VerticalAlignment="Center"
 EmptyContent="Enter Phone"
 Width="120" 
Margin="2"
Mask="(###) ###-####"
Value="{Binding Path=Customer.Phone, ValidatesOnDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
FontWeight="Bold"
AllowInvalidValues = "False"
 AutoFillNumberGroupSeparators = false;
  AutoFillZeros = "False"
FlowDirection = "LeftToRight"
 Precision = "0"
 HorizontalContentAlignment = "Left"
TextMode = "MaskedText"
SpinMode = "None"
/>

2 Answers, 1 is accepted

Sort by
0
Alex Fidanov
Telerik team
answered on 12 Mar 2012, 10:39 AM
Hello,

If you are binding to a string property, then I would suggest using the MaskedTextInput control and not the numeric one, as it is for numeric values. You would also not need the converter. Please note that the ValueToText converter is used to convert the Value to Text and be displayed as Text of the control, and not value. I believe that you need to apply the converter to the binding between your custom object and the mask's value.

All the best,
Alex Fidanov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Mike
Top achievements
Rank 1
answered on 14 Mar 2012, 05:17 AM
Thank you for the suggestion. I have tried this and it is working well.
Tags
MaskedInput (Numeric, DateTime, Text, Currency)
Asked by
Mike
Top achievements
Rank 1
Answers by
Alex Fidanov
Telerik team
Mike
Top achievements
Rank 1
Share this question
or