Why Textbox Can't Accept Chinese Input

卓凡 1 Reputation point
2022-07-04T15:55:33.667+00:00

hi there.
i use a textbox in WPF to display data, adn set it's IsReadOnly = true so that users can't modidy the data.
then i attached a eventhandle to it's MouseDoubleClick. In this eventhandle, i set the textbox 's IsReadOnly = false, so that users can modify the data.
but, why does it only accept keys in English InputMethod (eg. numbers, letters), except Chinese InputMethod ?

Waiting for resolve.

here is the Code:
<TextBox Name="tbx" IsReadOnly="True" MouseDoubleClick="TextBox_MouseDoubleClick" LostFocus="TextBox_LostFocus"/>

   private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)  
    {  
        TextBox textbox = (TextBox)sender;  
        textbox.IsReadOnly = false;  
    }  

    private void TextBox_LostFocus(object sender, RoutedEventArgs e)  
    {  
        TextBox tbx = (TextBox)sender;  
        tbx.IsReadOnly = true;  
    }  
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,762 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 48,511 Reputation points Microsoft Vendor
    2022-07-05T03:37:54.553+00:00

    Hi,@04443056. Welcome Microsoft Q&A.
    Based on my test, when the initial value of IsReadOnly of TextBox is set to true, Chinese cannot be input after changing to false. For this problem, it is recommended that you raise a question on Developer Community.
    If you could set the initial value of IsReadOnly to false, and then change its value as needed, you can try the following code.

    <Window x:Class="TextboxInput.MainWindow"  
        x:Name="window"  
    
            Title="MainWindow" Height="450" Width="800">  
        <StackPanel>  
            <Button Content="isReadonlyBinding" x:Name="isReadonlyBinding" Click="isReadonlyBinding_Click"></Button>  
            <TextBox Name="tbx" IsReadOnly="{Binding ElementName=window,Path=setread, Mode=OneWay}"  
                    Background="AliceBlue"  >  
    
            </TextBox>  
        </StackPanel>  
    </Window>  
    

    Codebehind:

     public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
        }  
    
        public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(  
        "setread",  
        typeof(bool),  
        typeof(MainWindow),  
        new PropertyMetadata(false)  
        );  
    
        public bool setread  
        {  
          get { return (bool)GetValue(IsReadOnlyProperty); }  
          set { SetValue(IsReadOnlyProperty, value); }  
    
        }  
    
        private void isReadonlyBinding_Click(object sender, RoutedEventArgs e)  
        {  
          if (setread == false)  
          {  
            setread = true;  
          }  
          else  
          {  
            setread=false;  
          }  
    
        }  
      }  
    

    217489-image.png


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

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments

  2. 卓凡 1 Reputation point
    2022-07-05T03:42:54.583+00:00

    thank you very much.
    your respone is a kind of resolve, but it doesn't meet my request.

    and now i find that this problem only occured on windows default inputmethod. if i install another inputmethod , it can accept Chinese Inpute.

    thank you a lot.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.