Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Silverlight > MaskedTextBox > Problem with nullable decimal

Answered Problem with nullable decimal

Feed from this thread
  • Steven avatar

    Posted on Sep 26, 2011 (permalink)

    I have an issue when I bind to the maskedtextbox using the numeric masktype and setting the mask to c. The property I'm binding to is a nullable decimal. I set an empty content so when the value is null, a text is shown. All works well until I clear my object (or rather I set it a to new instance of the same object). The empty content shows again so all looks well, but when I try to enter any value in the same boxes, nothing happens. I was able to reproduce in the following small project.

    Steps to reproduce:
    1. Enter a value in any text box(es)
    2. Click the 'clear boxes' button
    3. Enter a value in the same text box(es)
    4. Enter a value in a different text box and all works just fine (until you hit the 'clear boxes' button again)

    Here's my viewmodel:
    namespace MaskedTextBox
    {
        public class MainPageVM : INotifyPropertyChanged
        {
            private PropertyObject _propertyObjectProp;
     
            public PropertyObject PropertyObjectProp
            {
                get { return _propertyObjectProp; }
                set { _propertyObjectProp = value; OnPropertyChanged("PropertyObjectProp"); }
            }
     
            private readonly RelayCommand _clearCommand;
            public RelayCommand ClearCommand
            {
                get { return _clearCommand; }
            }
     
            public MainPageVM()
            {
                PropertyObjectProp = new PropertyObject();
                _clearCommand =  new RelayCommand(ClearProps, true);
            }
     
            private void ClearProps()
            {
                PropertyObjectProp = new PropertyObject ();
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
     
            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }

    This is the object I'm binding to:
    namespace MaskedTextBox
    {
        public class PropertyObject:INotifyPropertyChanged
        {
            private decimal? _prop1;
            public decimal? Prop1
            {
                get { return _prop1; }
                set { _prop1 = value; OnPropertyChanged("Prop1"); }
            }
     
            private decimal? _prop2;
            public decimal? Prop2
            {
                get { return _prop2; }
                set { _prop2 = value; OnPropertyChanged("Prop2"); }
            }
     
            private decimal? _prop3;
            public decimal? Prop3
            {
                get { return _prop3; }
                set { _prop3 = value; OnPropertyChanged("Prop3"); }
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
         
            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged!=null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }

    Here's my view:
    <UserControl x:Class="MaskedTextBox.MainPage"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
     
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="100"/>
            </Grid.RowDefinitions>
            <telerik:RadMaskedTextBox EmptyContent="Property 1" Grid.Row="0" MaskType="Numeric" Mask="c" Value="{Binding PropertyObjectProp.Prop1, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"/>
            <telerik:RadMaskedTextBox EmptyContent="Property 2" Grid.Row="1" MaskType="Numeric" Mask="c" Value="{Binding PropertyObjectProp.Prop2, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"/>
            <telerik:RadMaskedTextBox EmptyContent="Property 3" Grid.Row="2" MaskType="Numeric" Mask="c" Value="{Binding PropertyObjectProp.Prop3, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"/>
            <Button Grid.Row="3" Content="Clear boxes" Command="{Binding ClearCommand}"/>
        </Grid>
    </UserControl>

    And the code behind:
    namespace MaskedTextBox
    {
        public partial class MainPage : UserControl
        {
            private MainPageVM _viewModel;
     
            public MainPage()
            {
                _viewModel = new MainPageVM();
                InitializeComponent();
                DataContext = _viewModel;
            }
        }
    }

    Reply

  • Answer Alex Fioletov avatar

    Posted on Sep 28, 2011 (permalink)

    Hi Steven,

    I have the same issue with 2011.1.419.1040. What release do you use? Is the problem still in the 2011 Q2 SP1?
    By the way it's a problem with all nullable types, at least nullable double causes the same issue.

    I found 2 workarounds:
    - converter that converts nullable to regular decimal;
    - use NumericUpDown with ValueFormat="Currency" and hidden buttons. It has almost the same behavior with masked text box.

    Alex

    Reply

  • Steven avatar

    Posted on Sep 28, 2011 (permalink)

    Hi Alex,

    I'm using 2011.2.712.1040 and I didn't know about the other types.

    Your workarounds look promising but I can't use the first one because I use the EmptyContent property. A null is treated differently than a decimal (even 0.0).

    I'll try the NumericUpDown approach though.

    Thanks,

    Steven

    EDIT:
    This works for me:
    <telerik:RadNumericUpDown NullValue="Property 1" Grid.Row="0" Value="{Binding PropertyObjectProp.Prop1, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" ValueFormat="Currency" ShowButtons="False"/>

    Reply

  • Alex Fioletov avatar

    Posted on Sep 28, 2011 (permalink)

    Steven,

    There is newer release http://www.telerik.com/products/silverlight/whats-new/release-history/q2-2011-sp1-version-2011-2-0920.aspx
    But I do not see changes about masked text box. So there is almost no hope that it's fixed in the latest version.

    Lets wait and see what Telerik's  gurus think about it. May be we are doing something wrong).

    Best regards,
    Alex

    Reply

  • Petar Mladenov Petar Mladenov admin's avatar

    Posted on Sep 29, 2011 (permalink)

    Hello Alex Fioletov and Steven,

    We highly suggest you to use the new RadMaskedInput suite of controls where most of the issues in the RadMaskedTextBox are resolved (including the one from this thread) and new features are included. You can examine these demos here which are a good starting point. Please let us know if you need further assistance. 

    Regards,
    Petar Mladenov
    the Telerik team

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

    Reply

  • Alex Fioletov avatar

    Posted on Sep 29, 2011 (permalink)

    Hi Petar,

    Thank you for your suggestion. But I did not find how to do next things:

    1. If I do not want to see mask, how to turn it off?
    2. I do not want to restrict input length by mask. If you put mask #3.2 you are not able to enter 1000.00 or more. So if you does not want to restrict it by mask you have to put something like #20.2, but in this case you see a long mask.

    So, I do not want to see long mask, and in the other hand I do not want to restrict input by mask. Is there way to do it with new controls?

    I played with sample, I read the doc, but did not find how to do it. If you can provide small piece of XAML that demonstrates it, it will be great.

    Best regards,
    Alex

    Reply

  • Petar Mladenov Petar Mladenov admin's avatar

    Posted on Oct 4, 2011 (permalink)

    Hello Alex Fioletov,

     Unfortunately, this cannot be achieved in the RadMaskedInput controls. We have previously created a Feature Request in our PITS and you can vote for it , this way increasing its development priority. Feel free to ask if you need further assistance.

    Regards,
    Petar Mladenov
    the Telerik team

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

    Reply

  • Steven avatar

    Posted on Oct 4, 2011 (permalink)

    The mask issue is big one for me, so I'm still going with the RadNumericUpdown (which I marked as an answer). I voted on the feature request for the RadMaskedInput because if that wasn't such an ugly control, I would certainly use it.

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Silverlight > MaskedTextBox > Problem with nullable decimal
Related resources for "Problem with nullable decimal"

Silverlight MaskedTextBox Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  ]