WPF: how can we restrict TextBox only allow entry 25 Characters?

Shweta Goel 1 Reputation point

I am using Maxlength property of textbox in WPF UWP Windows app. This property is working on laptop in both PC and tablet mode but it is not working on touchscreen tablet.

How to implement maxlength peroperty for touchscreen tablet. Please suggest.

Universal Windows Platform (UWP)
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,663 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,851 Reputation points

    Hello @Shweta Goel , Welcome to Microsoft Q&A,

    During the testing, Maxlength property could work in touchscreen tablet device, please check if set the correct Maxlength value for the application. And if you want to delete text manually when the text length long than the Maxlength , you could use the InputInjector to delete it.

    private void TextBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)  
        if (sender.Text.Length > 15)  
            InputInjector inputInjector = InputInjector.TryCreate();  
            var info = new InjectedInputKeyboardInfo();  
            info.VirtualKey = (ushort)(VirtualKey.Back);  
            inputInjector.InjectKeyboardInput(new[] { info });  

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments