How to suppress Cut, Copy and Paste Operations in

2020-02-12 07:22发布

I want to suppress Cut, Copy and Paste operations in Text Box.

I don't want user to do any of these operations through keyboard or from default context menu in the text box .

Please let me know how can I restrict these operations?

1条回答
ら.Afraid
2楼-- · 2020-02-12 07:50

You can do this pretty easily using the CommandManager.PreviewCanExecute routed event. In your XAML, you would put the following on your TextBox element. This will apply to CTL+V, etc as well as the context menu or any buttons that you may have mapped to those commands so it's very effective.

<TextBox CommandManager.PreviewCanExecute="HandleCanExecute" />

Then in your code-behind, add a HandleCanExecute method that disables the commands.

private void HandleCanExecute(object sender, CanExecuteRoutedEventArgs e) {

    if ( e.Command == ApplicationCommands.Cut ||
         e.Command == ApplicationCommands.Copy ||
         e.Command == ApplicationCommands.Paste ) {

        e.CanExecute = false;
        e.Handled = true;

    }

}
查看更多
登录 后发表回答