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 ) 
    { 
    } 
} 

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 ); 
            } ); 
    } 
} 

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.