Changing inline in flowdocument

*爱你&永不变心* 提交于 2019-12-24 02:47:07

问题


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 on any part of that text.

First of all I am trying to change the background of a given inline (regardless of mouse click), but I am unable to do it.

Any help would be appreciated.

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


回答1:


You could add a Style for Run to subscribe e.g. MouseLeftButtonDown

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

and handle the event like this

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


来源:https://stackoverflow.com/questions/17906948/changing-inline-in-flowdocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!