在WPF VB.NET中禁用复制/粘贴/剪切

时间:2017-12-12 05:39:04

标签: wpf vb.net

我正在尝试在WPF 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不起作用:

  DataObject.AddPastingHandler(control, AddressOf Me.OnCancelCommand)
  DataObject.AddCopyingHandler(control, AddressOf Me.OnCancelCommand)

 private void OnCancelCommand(Object sender, DataObjectEventArgs e)
e.CancelCommand()

如何使第二个代码有效?

1 个答案:

答案 0 :(得分:0)

您可以重新绑定命令。这是一个C#版本,VB.Net应该是类似的

<TextBox>
    <TextBox.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Copy" CanExecute="CommandBinding_CanExecute"/>
        <CommandBinding Command="ApplicationCommands.Paste" CanExecute="CommandBinding_CanExecute"/>
    </TextBox.CommandBindings>
</TextBox>

处理程序:

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

考虑将ApplicationCommands.Cut添加到聚会中,除非您有不同的计划。