By clicking on TextBlock in WPF richtextbox

I have a flowdocument like this:

var mcFlowDoc = new FlowDocument(); var para = new Paragraph(); para.Inlines.Add(textBlock1); para.Inlines.Add(textBlock2); para.Inlines.Add(textBlock3); mcFlowDoc.Blocks.Add(para); richTextBox1.Document = mcFlowDoc; 

and I need an event to trigger a mouse click on a text block:

  <RichTextBox Margin="10,10,230,12" Name="richTextBox1" FontFamily="Simplified Arabic" FontSize="16" IsReadOnly="True" IsReadOnlyCaretVisible="False" ForceCursor="False" FlowDirection="RightToLeft" VerticalScrollBarVisibility="Auto"> <RichTextBox.Resources> <Style TargetType="Run"> <EventSetter Event="MouseLeftButtonDown" Handler="Run_Click" /> </Style> <Style TargetType="TextBlock"> <EventSetter Event="MouseLeftButtonDown" Handler="TextBlock_Click" /> </Style> </RichTextBox.Resources> </RichTextBox> void TextBlock_Click(object sender, MouseButtonEventArgs e) { TextBlock tb = sender as TextBlock; } 

The event handler for Run is called and works correctly ( Change inline in flowdocument ), but for TextBlock it is not.

What am I doing wrong? Thanks

+4
source share
1 answer

Quote from MSDN :

Attention!

RichTextBox has built-in processing for bubbling MouseUp and MouseDown events. Therefore, custom event handlers that listen for MouseUp or MouseDown events from RichTextBox will never be called. If you need to respond to these events, listen to the tunneling of the PreviewMouseUp and PreviewMouseDown events or register handlers with the HandledEventsToo argument (this last option is only available through code). Do not place the processed event unless you intend to disable RichTextBox native processing for these events, and remember that this has notable consequences for the control user interface.

So you need to look for alternatives. I can offer a few.

First, you can set the PreviewMouseDown event handler for all RichTextBox :

 <RichTextBox PreviewMouseDown="TextBlock_Click" ... /> 

Secondly, use BlockUIContainer and put the text in the content button. For instance:

 <Paragraph FontSize="18">Flow Example</Paragraph> <BlockUIContainer> <Button x:Name="MyButton" ClickMode="Release" Click="Button_Click"> <TextBlock Margin="4" TextWrapping="Wrap"> Some text </TextBlock> </Button> </BlockUIContainer> 

Thirdly, you can set the event handler for Paragraph as follows:

 var para = new Paragraph(); para.Inlines.Add(textBlock1); para.MouseLeftButtonDown += new MouseButtonEventHandler(TextBlock_Click); 

Edit

Quote from Adam Nathan's WPF 4 Unleashed book:

While a TextBox provides simple integer properties like CaretIndex, SelectionStart, and SelectionEnd, a RichTextBox provides a CaretPosition property of type TextPointer and a Selection property of type TextSelection. In addition, the contents of RichTextBoxs are stored in a Document property of type FlowDocument, and not in a simple text property Text. Content can even contain embedded UIElements, and they can be interactive and raise events if the RichTextBoxs IsDocumentEnabled property is set to true.

Events started to work, you need to add a BlockUIContainer with the IsDocumentEnabled property set to true (in RichTextBox ), otherwise the event will not work at all.

In general, I do not understand why you need this TextBlock inside a RichTextBox . Use its standard features that they almost cover, such as Run , Paragraph , etc. If they do not match, then there is no reason to use a RichTextBox .

See a good tutorial about RichTextBox here .

+5
source

Source: https://habr.com/ru/post/1493917/


All Articles