7

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 Answer 1

10

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;

    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.