How to suppress Cut, Copy and Paste Operations in TextBox in WPF?

耗尽温柔 提交于 2019-12-18 06:07:02

问题


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:


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;

    }

}


来源:https://stackoverflow.com/questions/3051144/how-to-suppress-cut-copy-and-paste-operations-in-textbox-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!