I have an array of sentences. Every sentence is a new Run object inside Inlines property of richtextbox`s FlowDocument. Every sentence have a color.
var paragraph = new Paragraph();
foreach (var sentence in Sentences)
{
....
paragraph.Inlines.Add(new Run { Text = sentence, Background = new SolidColorBrush(color) });
}
tbText.Document.Blocks.Add(paragraph);
When I am editing the sentence like this (I changed 'yes' to 'y1111111111111es' )
I expected to get the same Run object with changed text from 'yes' to 'y1111111111111es' but instead I got 3 Run objects with 'y', '1111111111111111111' and 'es'
That`s how I retrieve the textes
foreach (Paragraph paragraph in tbText.Document.Blocks)
{
foreach (Run inline in paragraph.Inlines)
{
editedTextes.Add(inline.Text);
}
}
Is there any way to edit the text inside native Run object without populating new Run objects when I change the text
It seems FlowDocument dynamically adds runs to encourage wrapping by design.
I see from another stackoverflow post , that if you use a TextBlock instead of runs inside of Paragraphs, you can preserve your text and remove the runs and prevent wrapping.
Try this in your second block:
foreach (Paragraph paragraph in tbText.Document.Blocks)
{
var sb = new StringBuilder();
foreach (Run inline in paragraph.Inlines)
{
sb.Append(inline.Text);
}
editedTextes.Add(new TextBlock()
{
Text = sb.ToString(),
TextWrapping = TextWrapping.NoWrap
});
}
Or just add one run after you have built the string if you don't like the TextBlocks:
editedTextes.Add(sb.ToString());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.