更改文本框IsReadOnly attr后,无法输入中文

时间:2019-04-10 06:57:39

标签: wpf winforms user-controls

在文本框上MouseDoubleClick可以编辑和输入,但不能输入中文,切换到其他窗口(或单击主窗口上的winform文本框)并返回时,现在可以输入中文。

演示:

https://img2018.cnblogs.com/q/22977/201904/22977-20190410130035963-221502838.gif

    <UserControl.Resources>
        <Style TargetType="TextBox">
            <Setter Property="IsReadOnly" Value="True"></Setter>
            <Setter Property="Width" Value="200"></Setter>
            <Setter Property="Height" Value="40"></Setter>
        </Style>
    </UserControl.Resources>
    <Grid>
        <TextBox InputMethod.IsInputMethodEnabled="True" InputMethod.PreferredImeState="On" MouseDoubleClick="TextBox_MouseDoubleClick"></TextBox>
    </Grid>
        private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var txtBox = (TextBox)sender;
            txtBox.IsReadOnly = false;
        }

更新:代码有效

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

            InputMethod.SetIsInputMethodEnabled(txtBox , false);
            InputMethod.SetIsInputMethodEnabled(txtBox , true);
        }

0 个答案:

没有答案