New to Telerik UI for WPF? Download free 30-day trial

Set MaxLength Property of the Input Area

The purpose of this tutorial is to show you how to set the MaxLength property of the InputArea of editable RadComboBox (RadComboBox's IsEditable property is set to True).

It is possible to set the MaxLength property of the InputArea using the following approaches:

Using the TextBoxStyle property

As of Q2 2014 release RadComboBox provides TextBoxStyle property which makes it easy to customize the TextBox part of the control. Just create a custom Style with TargetType set to TextBox and set the MaxLength to the needed value as shown below:

<UserControl.Resources> 
    <Style TargetType="TextBox" x:Key="CustomComboTextBoxStyle"> 
        <Setter Property="MaxLength" Value="5" /> 
    </Style> 
</UserControl.Resources> 

If you are using Implicit Styles to style the controls, you would need to base that Style to the default TextBoxStyle of RadComboBox named ComboBoxTextBoxStyle.

<Style TargetType="TextBox" x:Key="CustomComboTextBoxStyle" BasedOn="{StaticResource ComboBoxTextBoxStyle}"> 
    ... 
</Style> 

Then, set the created Style as TextBoxStyle of RadComboBox:

<telerik:RadComboBox         
    IsEditable="True"  
    TextBoxStyle="{StaticResource CustomComboTextBoxStyle}" /> 

Using the control Resources

By accessing the ComboBox resources it is possible to modify the Style of the TextBox in the control and set its MaxLength property.

The next code snippet shows how to set the MaxLength property of the TextBox in the control using its resources:

<telerik:RadComboBox IsEditable="True"> 
    <telerik:RadComboBox.Resources> 
        <Style TargetType="TextBox"> 
            <Setter Property="MaxLength" Value="5" /> 
        </Style> 
    </telerik:RadComboBox.Resources> 
</telerik:RadComboBox> 

Using an Attached property

The idea in this case is to create a new Attached Property in a helper class.

public class EditableComboBox 
{ 
    public static readonly DependencyProperty MaxLengthProperty = 
        DependencyProperty.RegisterAttached( "MaxLength", typeof( int ), typeof( EditableComboBox ), new PropertyMetadata( OnMaxLengthChanged ) ); 
 
    public static int GetMaxLength( DependencyObject obj ) 
    { 
        return ( int )obj.GetValue( MaxLengthProperty ); 
    } 
 
    public static void SetMaxLength( DependencyObject obj, int value ) 
    { 
        obj.SetValue( MaxLengthProperty, value ); 
    } 
 
    private static void OnMaxLengthChanged( DependencyObject obj, DependencyPropertyChangedEventArgs e ) 
    { 
    } 
} 
Public Class EditableComboBox 
    Public Shared ReadOnly MaxLengthProperty As DependencyProperty = DependencyProperty.RegisterAttached("MaxLength", GetType(Integer), GetType(EditableComboBox), New PropertyMetadata(OnMaxLengthChanged)) 
 
    Public Shared Function GetMaxLength(ByVal obj As DependencyObject) As Integer 
        Return CInt(obj.GetValue(MaxLengthProperty)) 
    End Function 
 
    Public Shared Sub SetMaxLength(ByVal obj As DependencyObject, ByVal value As Integer) 
        obj.SetValue(MaxLengthProperty, value) 
    End Sub 
 
    Private Shared Sub OnMaxLengthChanged(ByVal obj As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs) 
    End Sub 
End Class 

In the OnMaxLengthChanged() event handler you should perform the following step.

  • Get the target combobox.

  • Get the PART_EditableTextBox element from its template.

  • The PART_EditableTextBox is a TextBox and you can easily set its MaxLength property.

public class EditableComboBox 
{ 
    public static readonly DependencyProperty MaxLengthProperty = 
        DependencyProperty.RegisterAttached( "MaxLength", typeof( int ), typeof( EditableComboBox ), new PropertyMetadata( OnMaxLengthChanged ) ); 
 
    public static int GetMaxLength( DependencyObject obj ) 
    { 
        return ( int )obj.GetValue( MaxLengthProperty ); 
    } 
 
    public static void SetMaxLength( DependencyObject obj, int value ) 
    { 
        obj.SetValue( MaxLengthProperty, value ); 
    } 
 
    private static void OnMaxLengthChanged( DependencyObject obj, DependencyPropertyChangedEventArgs e ) 
    { 
        var comboBox = obj as Telerik.Windows.Controls.RadComboBox; 
        if ( comboBox == null ) 
            return; 
 
        comboBox.Dispatcher.BeginInvoke( 
            delegate() 
            { 
                var rootElement = VisualTreeHelper.GetChild( comboBox, 0 ) as FrameworkElement; 
                TextBox textBox = ( TextBox )rootElement.FindName( "PART_EditableTextBox" ); 
                if ( textBox != null ) 
                     textBox.SetValue( TextBox.MaxLengthProperty, e.NewValue ); 
            } ); 
    } 
} 
Public Class EditableComboBox 
    Public Shared ReadOnly MaxLengthProperty As DependencyProperty = DependencyProperty.RegisterAttached("MaxLength", GetType(Integer), GetType(EditableComboBox), New PropertyMetadata(OnMaxLengthChanged)) 
    Public Shared Function GetMaxLength(ByVal obj As DependencyObject) As Integer 
        Return CInt(obj.GetValue(MaxLengthProperty)) 
    End Function 
    Public Shared Sub SetMaxLength(ByVal obj As DependencyObject, ByVal value As Integer) 
        obj.SetValue(MaxLengthProperty, value) 
    End Sub 
    Private Shared Sub OnMaxLengthChanged(ByVal obj As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs) 
        Dim comboBox = TryCast(obj, Telerik.Windows.Controls.RadComboBox) 
        If comboBox Is Nothing Then 
            Exit Sub 
        End If 
        comboBox.Dispatcher.BeginInvoke(Function() Do 
            Dim rootElement = TryCast(VisualTreeHelper.GetChild(comboBox, 0), FrameworkElement) 
            Dim textBox As TextBox = DirectCast(rootElement.FindName("PART_EditableTextBox"), TextBox) 
            If textBox IsNot Nothing Then 
                textBox.SetValue(TextBox.MaxLengthProperty, e.NewValue) 
            End If  
       End Function) 
    End Sub 
End Class 

Finally set the property in XAML:

<telerik:RadComboBox IsEditable="True" example:EditableComboBox.MaxLength="20"/> 

Note that you have to set the RadComboBox's IsEditable property to True.

See Also

In this article