4

I am having some issues with the WPF RichTextBox. I have created a behavior which works on both a normal TextBox, and a RichTextBox. In the behavior, I am using two events:
- OnTextChanged
- OnPreviewKeyDown

I assumed that OnPreviewKeyDown always fires before OnTextChanged when typing text.

The behavior works like it should for a normal TextBox. However, the event order of the WPF RichTextBox's PreviewKeyDown and OnTextChanged seems to differ from the normal TextBox's event order. The following code reproduces the problem by typing "as " very quickly, or even by pressing the 3 keys at the same time:

TextBox

class TestTextBox : TextBox
{
    protected override void OnTextChanged(TextChangedEventArgs e)
    {
        base.OnTextChanged(e); // Breakpoint prints "TextChanged!"
    }

    protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
    {
        base.OnPreviewKeyDown(e); // Breakpoint prints the pressed key
    }
}

Output:
A
TextChanged!
S
TextChanged!
Space
TextChanged!

RichTextBox:

class TestRichTextBox : RichTextBox
{
    protected override void OnTextChanged(TextChangedEventArgs e)
    {
        base.OnTextChanged(e); // Breakpoint prints "TextChanged!"
    }

    protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
    {
        base.OnPreviewKeyDown(e); // Breakpoint prints the pressed key
    }
}

Output:
A
S
Space
TextChanged
TextChanged
TextChanged

Basically the problem is that my behavior assumes the eventorder of the normal TextBox. Obviously, this causes problems when I use the same behavior for a RichTextBox.

  • Why does the event order differ for the RichTextBox in the first place?
  • Could the event order of the normal TextBox also differ from the assumed order?
  • Can I somehow force the event order, or reach the same result in some other way?
Suresh Kumar Veluswamy
  • 4,193
  • 2
  • 20
  • 35
Olrox
  • 41
  • 4
  • Would it be because of the flow document capabilities of the `RichTextBox` i.e. formatting and pagination? And the `TextBox` has only got simple display capabilities. So the events are fired normally for `TextBox` while `RichTextBox` needs to perform some actions and check internally. – XAMlMAX Mar 28 '14 at 09:17
  • `OnPreviewKeyDown` still fires before the `OnTextChanged` right. Yes granted if you have a N key rollover keyboard and you spam keys, multiple keydown's are registered before each textchanged is processed. However I'm not sure where I'm seeing TextChanged getting fired before it's corresponding KeyDown event. Or do you mean your problem is to get a keydown immediately followed by a TextChanged before another keydown? – Viv Mar 28 '14 at 10:20
  • 1
    Yes, I expected every PreviewKeyDown event call to be followed by the TextChanged event before another PreviewKeyDown. It seems to work that way for the normal TextBox (at least I have no scenario which indicates otherwise) – Olrox Mar 28 '14 at 10:36

1 Answers1

0

These two events in certain sense trigger same flow of actions so why not use

protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
{
    base.OnPreviewKeyDown(e); // Breakpoint prints the pressed key
}

as a starting point for you logic (since it always fires first), and leave the OnTextChanged alone?

Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265