WPF下Textbox有关知识

1.Textbox如何禁用复制粘贴和剪切

2.TextBox如何输入中文


1.Textbox如何禁用复制粘贴和剪切

XMAL文件: 

<TextBox x:Name="Rate"  Style="{StaticResource Input }" Margin="344,135,336,0"  PreviewTextInput="Rate_PreviewTextInput" 
			                 	IsEnabled="False" RenderTransformOrigin="0.617,1.2"   MaxLength="6">
    <TextBox.CommandBindings>
        <CommandBinding  Command="ApplicationCommands.Paste" CanExecute="CommandBinding_CanExecute"/>
        <CommandBinding  Command="ApplicationCommands.Copy" CanExecute="CommandBinding_CanExecute"/>
        <CommandBinding  Command="ApplicationCommands.Cut" CanExecute="CommandBinding_CanExecute"/>
    </TextBox.CommandBindings>
</TextBox>

cs文件下

  private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
            e.Handled = true;
        }

这里涉及到了WPF的一个小知识:WPF命令绑定时多个控件可以绑定同一个事件,这样省去了许多冗余的代码(适用于多个控件增加同一种限制)。

2.TextBox如何输入中文

 <TextBox x:Name="UserName" Style="{StaticResource Input}" Margin="295,55,0,0" 
InputLanguageManager.InputLanguage="zh-CN" 
 InputMethod.IsInputMethodEnabled="True" TextChanged="UserName_TextChanged"/>

我这里因为在text的样式模板中设置的 InputMethod.IsInputMethodEnabled="False"的属性。这个属性的意思是禁用输入法。如果我们想输入中文的话,需要将这个属性打开,才可以使用输入法。也就是设置 InputMethod.IsInputMethodEnabled="True",这个属性是最主要的。而InputLanguageManager.InputLanguage="zh-CN" 此代码意思是当你控件获得光标时,他会控制输入法的首选语言。

更多有关Text输入限制等知识,请点这里。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值