When copy-pasting an IP address from a share-point site, I can see that the actual value that gets pasted in the text-box is: 123.123.123.123U+200B
, which then returns false when attempting to IPAddress.TryParse(...)
.
I'm rather new in WPF world, and with the help of some SO posts I have so far managed to resolve this by having the PasteHandler in my code-behind class:
private static void TextPasteHandler(object sender, DataObjectPastingEventArgs e)
{
if (!e.DataObject.GetDataPresent(typeof(string))) return;
var pastedText = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
DataObject dObj = new DataObject();
dObj.SetData(DataFormats.Text, Regex.Replace(pastedText, @"[^\u0020-\u007E]", string.Empty));
e.DataObject = dObj;
}
However, I was wondering if there's a more standard WPF way to handle cases like this?