Paste Event in a WPF TextBox

For backspace, please check the PreviewKeyDown event

For paste command, add a command binding to the ApplicationCommands.Paste, and set the argument to handled, if you do not wish to do anything with it:

<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Paste"
                  Executed="PasteExecuted" />
</Window.CommandBindings>

And in code behind:

private void PasteExecuted(object sender, ExecutedRoutedEventArgs e)
{
    e.Handled = true;
}

Here's some code I had lying around in case I ever needed it. Might help you.

public Window1()
{
    InitializeComponent();

    // "tb" is a TextBox
    DataObject.AddPastingHandler(tb, OnPaste);
}

private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
    var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
    if (!isText) return;

    var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
    ...
}

This might not be the exact answer your looking for but here is how to handle pasted text (this also works if user pasted using a the context menu):

InitializeComponent();

                // "DescriptionTextBox" is a TextBox
                DataObject.AddPastingHandler(DescriptionTextBox, OnDescriptionPaste);

private void OnDescriptionPaste(object sender, DataObjectPastingEventArgs e)
        {
            if (!e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true))
                return;

            var pastedText = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
            if (string.IsNullOrEmpty(pastedText))
                return;

            var txtBox = (TextBox) sender;

            var before = ""; //Text before pasted text
            var after = txtBox.Text; //Text after pasted text

            //Get before and after text
            if (txtBox.CaretIndex > 0)
            {
                before = txtBox.Text.Substring(0, txtBox.CaretIndex);
                after = txtBox.Text.Substring(txtBox.CaretIndex);
            }

            //Do custom logic for handling the pasted text.
            //Split sentences ending with . into new line.
            var parts = pastedText.Split(new []{'.'}, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length > 1)
            {
                pastedText = parts.Select(x => x.Trim()).ToArray().ToStringX(".\r\n");
                pastedText += ".";
            }

            var newCaretIndex = before.Length + pastedText.Length;

            e.CancelCommand(); //Cancels the paste, we do it manually
            txtBox.Text = $"{before}{pastedText}{after}"; //Set new text
            txtBox.CaretIndex = newCaretIndex; //Set new caret index
        }

For handling backspace use PreviewKeyDown event.


The trouble with trying to intercept and trap all the individual events that might cause a TextBox.Text property to change is that there are many such events:

  • TextInput: User types
  • KeyDown: Delete, Backspace, Enter, IME
  • Command Gestures: Ctrl-X, Ctrl-Y, Ctrl-V, Ctrl-X
  • MouseDown: Paste button, Cut button, Undo button, ...
  • Click: Space bar pressed when Paste, Cut, Undo buttons have local focus
  • RaiseEvent: Code raises Paste, Cut, Undo, Redo commands
  • Accessiblity: Voice commands, Braille keyboards, etc

Trying to reliably intercept all of these is an exercise in futility. A much better solution is to monitor TextBox.TextChanged and reject changes that you don't like.

In this answer I show how to implement a TextBoxRestriction class for the particular scenario being asked about. This same technique can be generalized for use with any restrictions you want to place on your TextBox control.

For example, in your case you might implemnt a RestrictValidChars attached property similarly to the RestrictDeleteTo property in that code. It would be the same except that the inner loop would check inserts, not deletes. It would be used like this:

<TextBox my:TextBoxRestriction.RestrictValidChars="0123456789" />

This is just an idea of how it could be handled. There are many ways to structure your code depending on what you want. For example you could change TextBoxRestriction to call your own code to validate using an attached property that takes a delegate or an object containing an event.

See the other answer for details on how to bind the Text property when you are using the TextBoxRestriction class so it won't trigger the restriction when you don't want it to.