Change inline in flowdocument

I have a flowdocument like this:

var mcFlowDoc = new FlowDocument(); var para = new Paragraph(); para.Inlines.Add(new Run("This is the first line.")); para.Inlines.Add(new Run("This is the second line.")); para.Inlines.Add(new Run("This is the third line.")); mcFlowDoc.Blocks.Add(para); richTextBox.Document = mcFlowDoc; 

I need to change the background of one of the lines by clicking any part of this text.

First of all, I am trying to change the background of a given line (regardless of the mouse click), but I cannot do this.

Any help would be appreciated.

Edit: I could change the background color of the run, but then I had to add all the runs again and redraw. I need it to work faster, so I'm just trying to change the execution without adding everything again.

+1
source share
1 answer

You can add Style for Run to subscribe, for example. MouseLeftButtonDown

 <RichTextBox.Resources> <Style TargetType="Run"> <EventSetter Event="MouseLeftButtonDown" Handler="Run_Click" /> </Style> </RichTextBox.Resources> 

and handle an event like this

 void Run_Click(object sender, MouseButtonEventArgs e) { Run run = sender as Run; run.Background = Brushes.Red; } 
+1
source

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


All Articles