Make WPF textbox as cut, copy and paste restricted

Sauron picture Sauron · Jun 2, 2009 · Viewed 26.4k times · Source

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

Answer

Prashant Cholachagudda picture Prashant Cholachagudda · Jun 2, 2009

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

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;
     }
}