Binding MaskedText property of Telerik Silverlight RadMaskedTextBox

If you have used Telerik’s RadMaskedTextBox for Silverlight most probably in some cases you have needed to bind in TwoWay mode the MaskedText property of the control (for example for Phones, SSNs, etc. that need to be saved with the punctuation). And most probably you were surprised that you cannot do this because the MaskedText property is readonly. We’ve asked Telerik whether they are considering to implement such a functionality, but currently there is no definite answer to this.

So here is how to extend they RadMaskedTextBox so you can do your work:

VB:

Imports System.Windows.Data
Imports Telerik.Windows

Public Class RadMaskedTextBoxExtended
    Inherits Telerik.Windows.Controls.RadMaskedTextBox

    Private m_bIsValueChangingIn As Boolean = False

#Region " Dependency Property "
    Public Shared ReadOnly ValueMaskProperty As DependencyProperty = _
    DependencyProperty.Register("ValueMask" _
                                , GetType(String) _
                                , GetType(RadMaskedTextBoxExtended) _
                                , New PropertyMetadata(AddressOf ValueMaskChanged) _
                                )
#End Region

#Region " Constructor "
    Public Sub New()
        MyBase.New()
        AddHandler Me.ValueChanged, AddressOf OnValueChanged
    End Sub
#End Region

#Region " Properties "
    Public Property ValueMask() As String
        Get
            Return CStr(Me.GetValue(ValueMaskProperty))
        End Get
        Set(ByVal value As String)
            Me.SetValue(ValueMaskProperty, value)
        End Set
    End Property

    Public Property IsValueChangingIn() As Boolean
        Get
            Return m_bIsValueChangingIn
        End Get
        Set(ByVal value As Boolean)
            m_bIsValueChangingIn = value
        End Set
    End Property
#End Region

#Region " Change Tracking Events "
    Private Sub OnValueChanged(ByVal sender As Object, ByVal e As RadRoutedEventArgs)
        m_bIsValueChangingIn = True
        Me.ValueMask = Me.MaskedText
        Dim oBindingExpression As BindingExpression = Me.GetBindingExpression(ValueMaskProperty)

        If oBindingExpression IsNot Nothing Then
            oBindingExpression.UpdateSource()
        End If
        m_bIsValueChangingIn = False
    End Sub

    Private Shared Sub ValueMaskChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim txtRadMaskedTextBoxExtended As RadMaskedTextBoxExtended = CType(d, RadMaskedTextBoxExtended)

        If Not Equals(e.OldValue, e.NewValue) _
        AndAlso Not txtRadMaskedTextBoxExtended.IsValueChangingIn _
        Then
            txtRadMaskedTextBoxExtended.Value = e.NewValue
        End If
    End Sub
#End Region

End Class

Then instead of using their control, just use RadMaskedTextBoxExtended and use the ValueMask property to bind the business object property that must contain the masked text.

This worked quite well in my case 🙂
Enjoy!