1

I have a TextBox, and a TextBlock within the Border. The TextBlock's Text property is bound to TextBox's value. When I type into the TextBox, the Border changes its width according to the TextBlock's new size.

There is an event handler for TextBox.TextChanged in which I test whether the size of the border exceeds a certain number. If it does, I want to prevent the TextBox from making the change that caused the handler.

If a character was always added to an end, I would be able just to substring the text, but all other sorts of changing can occur, for example pasting a large amount of text into the TextBox.

So, what would be the way of preventing the change from the handler? I remember in some WindowsForms e.Cancel property which when set would ignore the action, but haven't seen that in WPF and the TextChangedEventArgs obviously does not have one.

Thanks

Kornelije Petak
  • 9,412
  • 15
  • 68
  • 96

1 Answers1

0

You can listen to the PreviewTextInput event and set e.Handled to true to prevent the change from taking effect.

MetaColon
  • 2,895
  • 3
  • 16
  • 38