我有一个由keyDown事件管理的文本框,它限制插入4个字符,但是当用户复制/粘贴它时,DePace 4字符!所以可以禁止这个选项。
提前谢谢
答案 0 :(得分:2)
<TextBox CommandManager.PreviewExecuted="textBox_PreviewExecuted"/>
<强> C#强>
private void textBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
{
if (e.Command == ApplicationCommands.Copy ||
e.Command == ApplicationCommands.Cut ||
e.Command == ApplicationCommands.Paste)
{
e.Handled = true;
}
}
<强> VB.NET 强>
Private Sub textBox_PreviewExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
If e.Command Is ApplicationCommands.Copy
OrElse e.Command Is ApplicationCommands.Cut
OrElse e.Command Is ApplicationCommands.Paste Then
e.Handled = True
End If
End Sub
要使用键禁用(Ctrl + C,Ctrl + V),请使用以下代码:
<强> C#强>
DataObject.AddPastingHandler(control, this.OnCancelCommand);
DataObject.AddCopyingHandler(control, this.OnCancelCommand);
private void OnCancelCommand(object sender, DataObjectEventArgs e)
{
e.CancelCommand();
}
<强> VB.NET 强>
DataObject.AddPastingHandler(control, AddressOf Me.OnCancelCommand)
DataObject.AddCopyingHandler(control, AddressOf Me.OnCancelCommand)
private void OnCancelCommand(Object sender, DataObjectEventArgs e)
e.CancelCommand()