将WPF文本框设置为剪切、复制和粘贴受限

时间:2022-09-28 23:04:13

How can I make a WPF textbox cut, copy and paste restricted?

如何使WPF文本框剪切、复制和粘贴受限?

2 个解决方案

#1


40  

Cut, Copy and Paste are the common commands used any application,

剪切、复制和粘贴是常用的命令,

<TextBox CommandManager.PreviewExecuted="textBox_PreviewExecuted"
         ContextMenu="{x:Null}" />

in above textbox code we can restrict these commands in PrviewExecuted event of CommandManager Class

在上面的文本框代码中,我们可以在CommandManager类的prviewexecute事件中限制这些命令

and in code behind add below code and your job is done

在代码后面添加下面的代码,你的工作就完成了

private void textBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
{
     if (e.Command == ApplicationCommands.Copy ||
         e.Command == ApplicationCommands.Cut  || 
         e.Command == ApplicationCommands.Paste)
     {
          e.Handled = true;
     }
}

#2


13  

The commandName method will not work on a System with Japanese OS as the commandName=="Paste" comparision will fail. I tried the following approach and it worked for me. Also I do not need to disable the context menu manually.

由于commandName=="Paste" comparision失败,因此commandName方法不能在使用日语操作系统的系统上工作。我尝试了下面的方法,它对我很有效。另外,我不需要手动禁用上下文菜单。

In the XaML file:

在XaML文件:

<PasswordBox.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Paste"
    CanExecute="CommandBinding_CanExecutePaste"></CommandBinding>
</PasswordBox.CommandBindings>

In the code behind:

在背后的代码:

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

#1


40  

Cut, Copy and Paste are the common commands used any application,

剪切、复制和粘贴是常用的命令,

<TextBox CommandManager.PreviewExecuted="textBox_PreviewExecuted"
         ContextMenu="{x:Null}" />

in above textbox code we can restrict these commands in PrviewExecuted event of CommandManager Class

在上面的文本框代码中,我们可以在CommandManager类的prviewexecute事件中限制这些命令

and in code behind add below code and your job is done

在代码后面添加下面的代码,你的工作就完成了

private void textBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
{
     if (e.Command == ApplicationCommands.Copy ||
         e.Command == ApplicationCommands.Cut  || 
         e.Command == ApplicationCommands.Paste)
     {
          e.Handled = true;
     }
}

#2


13  

The commandName method will not work on a System with Japanese OS as the commandName=="Paste" comparision will fail. I tried the following approach and it worked for me. Also I do not need to disable the context menu manually.

由于commandName=="Paste" comparision失败,因此commandName方法不能在使用日语操作系统的系统上工作。我尝试了下面的方法,它对我很有效。另外,我不需要手动禁用上下文菜单。

In the XaML file:

在XaML文件:

<PasswordBox.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Paste"
    CanExecute="CommandBinding_CanExecutePaste"></CommandBinding>
</PasswordBox.CommandBindings>

In the code behind:

在背后的代码:

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