Handle Clipboard Paste in WPF
public YourWindow() { InitializeComponent(); // "yourTextBox" is your TextBox DataObject.AddPastingHandler(yourTextBox, 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; // Manipulate the text here text = text.Replace("oldValue", "newValue"); // Example manipulation // Set the new data e.DataObject = new DataObject(DataFormats.UnicodeText, text); }
Note that this event is triggered after the user initiates the paste command but before the content is actually pasted into the TextBox, allowing you to modify or cancel the paste operation.
References
https://stackoverflow.com/questions/3061475/paste-event-in-a-wpf-textbox