0

I have several textbox fields in my WPF application that have max character lengths set.

The Problem:

If you paste a string that is too long, then it truncates that string to fit the max length.

The Question:

How can I check that on pasting to a textbox control if the character count for contents that is being pasted is greater than the max character length set for that textbox?

Scott P
  • 88
  • 1
  • 9

1 Answers1

0

Take a look at Paste Event in a WPF TextBox

You can use a solution similar to the accepted answer, where you add a paste handler, and compare the MaxLength of the TextBox to the contents of the clipboard:

var tb = sender as TextBox;
var text = e.SourceDataObject.GetData(DataFormats.Text) as string;
if(text > tb.MaxLength) {
    //Do whatever it is you want to do
}
Community
  • 1
  • 1
Troels Larsen
  • 4,462
  • 2
  • 34
  • 54