This project has moved and is read-only. For the latest updates, please go here.

MaskedTextBox Value Binding

Nov 14, 2012 at 2:31 PM

Hi guys, I'm hoping y'all can help me out with an issue I'm having with the MaskedTextBox.  I'm using Version 1.8 of the toolkit with an MVVM architecture.

The issue I'm having is that the Value binding will not update unless the entire mask has been met.  This is causing issues (particularly with validation and data persistence) on certain fields.

In this case I have a Phone # field (not required) that is bound to a property with a custom Data Annotation attribute applied to validate that the phone number meets our requirements.  This validation never kicks in until the entire MaskedTextBox field has been filled in.

For example, if the user enters "1234", the field is displayed as "(123) 4__-____" (which is what I want).  My property still has a null value though, so not only is the "1234" not persisted in the property the textbox is bound to, but the Phone Number validation never fires to let the user know the field is not correct.  There is also no indication that the bound property still has a null value even though the textbox does have text entered.

Is there any way around this issue?  Something that I'm doing incorrectly?  The relevant parts of my code are shown below:


public string PhoneNumber
    get { return this._phoneNumber; }
        this._phoneNumber = value;


<xctk:MaskedTextBox Value="{Binding PhoneNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                    Mask="(000) 000-0000"
                    ValueDataType="{x:Type sys:String}"/>

Nov 15, 2012 at 1:56 AM

In your mask, "0" mean a mandatory number, and "9" an optional one. Maybe your mask should be:

"(999) 999-9999"

Nov 15, 2012 at 1:13 PM

Thank you emartin! Changing the mask to use 9's rather than 0's made things work the way I expected them to.