Is there any more elegant way to do the following?
Basically I need an easy way to programatically build a WrapPanel (or other FrameworkElement) that:
- wraps correctly
- allows some words to have bold text
- allows some words to have italic text
- allows other formatting, e.g. color, background
- ideal would be some method that converts e.g. “
This is <b>bold</b> and this is <i>italic</i> text.” into an appropriate FrameworkElement so I can e.g. add it to a StackPanel and display it.
Code:
using System.Windows;
using System.Windows.Controls;
namespace TestAddTextBlock2343
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
WrapPanel wp = new WrapPanel();
wp.AddTextBlock("This is a sentence with ");
{
TextBlock tb = wp.AddTextBlockAndReturn("bold text");
tb.FontWeight = FontWeights.Bold;
}
wp.AddTextBlock(" and ");
{
TextBlock tb = wp.AddTextBlockAndReturn("italic text");
tb.FontStyle = FontStyles.Italic;
}
wp.AddTextBlock(" in it.");
}
}
public static class XamlHelpers
{
public static TextBlock AddTextBlockAndReturn(this WrapPanel wp, string text)
{
TextBlock tb = new TextBlock();
tb.Text = text;
wp.Children.Add(tb);
return tb;
}
public static void AddTextBlock(this WrapPanel wp, string text)
{
TextBlock tb = wp.AddTextBlockAndReturn(text);
}
}
}
Edit: I discovered in a different answer that the
TextBlockalso has anInlinescollection to which one can addRuns. Anvaka’s answer ingeniously uses an attached property as a sort of converter.What I think will suit your situation is a
FlowDocumentScrollViewerand aFlowDocument. I describe manual creation of one through anIValueConvertera little bit here.You’ll likely use similar helper functions as you’ve shown in your example, but the
FlowDocumentis already much like HTML and will handle wrapping effortlessly.You add
Paragraphs to theFlowDocument, you addRuns to theParagraph, and eachRunderives fromTextElementso it has a lot of the same properties thatTextBlocks do.Also, if the formatting substrings are going to remain restricted to bold/italic tags or some other extremely simple markup, use of
Regex.Split()might be the easiest way to determine the separateRuns from a single string. It allows you to split a string into multiple strings but keep your “delimiters”.