WPF paragraph keydown not firing

360 views Asked by At

I have added KeyDown, PreviewKeyDown event (and some more) to a programmatically created Paragraph:

        Paragraph p = new Paragraph();
        Run r = new Run("new Text here");
        p.Inlines.Add(r);
        p.KeyDown += new KeyEventHandler(p_KeyDown);
        p.PreviewKeyDown += new KeyEventHandler(p_PreviewKeyDown);
        p.PreviewTextInput += new TextCompositionEventHandler(p_PreviewTextInput);
        p.TextInput += new TextCompositionEventHandler(p_TextInput);
        myrtb.Document.Blocks.Add(p);

But none of them is firing when I change the text ("new Text here") in the RichTextBox. Does anyone know what I'm doing wrong here? Many thanks!

1

There are 1 answers

2
Michal Ciechan On

As far as i can tell, the RichTextBox does not route KeyDown events to the current selected Block/Paragraph, therefore you will have to handle the RTB events, and get the current Paragraph by using the current CaretPosition.