WPF 设置输入只能英文

有时输入只能让用户输入英文,那么如何设置输入只能英文?
<!--more-->

首先在xaml 写一个 TextBox ,给他一个名字。

            <TextBox x:Name="txt"></TextBox>

然后在构造使用 System.Windows.Input.InputMethod 可以设置 IME 和输入是否可以是中文。

                System.Windows.Input.InputMethod.SetIsInputMethodEnabled(txt, false);

设置IME关掉

                InputMethod.SetPreferredImeState(txt,InputMethodState.Off);

当然也可以在页面写

            <TextBox InputMethod.IsInputMethodEnabled="False"></TextBox>

注意用户可以粘贴中文,可以检测用户是否输入有中文。

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split(' ').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
原文地址:https://www.cnblogs.com/lindexi/p/6949672.html