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" 此代码意思是当你控件获得光标时,他会控制输入法的首选语言。