如何在 WPF 中的 TextBox 中抑制剪切、复制和粘贴操作? [英] How to suppress Cut, Copy and Paste Operations in TextBox in WPF?
问题描述
我想取消文本框中的剪切、复制和粘贴操作.
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?
推荐答案
您可以使用 CommandManager.PreviewCanExecute 路由事件轻松完成此操作.在您的 XAML 中,您会将以下内容放在您的 TextBox 元素上.这将适用于 CTL+V 等以及上下文菜单或您可能已映射到这些命令的任何按钮,因此它非常有效.
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" />
然后在您的代码隐藏中,添加一个禁用命令的 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;
}
}
这篇关于如何在 WPF 中的 TextBox 中抑制剪切、复制和粘贴操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!