Suggestion will be to manually construct your FlowDocument
with the new formating (you can check the MSDN Magazine August 2007: WPF
Flexible Content Display With Flow Documents; or the most recent MSDN article Flow Document Overview ), which will improve the performance dramatically, e.g. use your example if do it manually as below, on my machine it will get the result in 52 ms, where as using ApplyPropertyValue
will take 1266 ms:
private readonly SolidColorBrush _blueBrush = Brushes.Blue;
private void ApplyFormatClick(object sender, RoutedEventArgs e)
{
Stopwatch stopwatch = Stopwatch.StartNew();
FlowDocument doc = Rtb.Document;
TextRange range = new TextRange(doc.ContentStart, doc.ContentEnd);
Paragraph para = new Paragraph();
string rangetem = range.Text;
range.ClearAllProperties();
for(int i=0; i<rangetem.Count();i+=2)
{
Span s = new Span() { Foreground = _blueBrush };
Bold b = new Bold();
s.Inlines.Add(rangetem[i].ToString());
b.Inlines.Add(s);
para.Inlines.Add(b);
if(i+1<rangetem.Count())
{
para.Inlines.Add(rangetem[i + 1].ToString());
}
}
doc.Blocks.Clear();
doc.Blocks.Add(para);
Time.Text = "Formatting took: " + stopwatch.ElapsedMilliseconds + " ms, number of characters: " + range.Text.Length;
}