3

I need to set the text of a TextBlock code behind to a string containing formatted text.

For example this string:

"This is a <Bold>message</Bold> with bold formatted text"

If I put this text in xaml file in this way it work correctly

<TextBlock>
  This is a <Bold>message</Bold> with bold formatted text
</TextBlock>

But if I set it using the Text property don't work.

string myString = "This is a <Bold>message</Bold> with bold formatted text";
myTextBlock.Text = myString;

I know I can use Inlines:

myTextBlock.Inlines.Add("This is a");
myTextBlock.Inlines.Add(new Run("message") { FontWeight = FontWeights.Bold });
myTextBlock.Inlines.Add("with bold formatted text");

But the problem is that I get the string as it is from another source and I have no idea how I can pass this string to the TextBlock and see if formatted. I hope there is a way to set the content of the TextBlock directly with the formatted string, because I have no idea of how I can parse the string to use it with Inlines.

user2272143
  • 469
  • 5
  • 22

2 Answers2

5

You may parse a TextBlock from your string and return a collection of its Inlines:

private IEnumerable<Inline> ParseInlines(string text)
{
    var textBlock = (TextBlock)XamlReader.Parse(
        "<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
        + text
        + "</TextBlock>");

    return textBlock.Inlines.ToList(); // must be enumerated
}

Then add the collection to your TextBlock:

textBlock.Inlines.AddRange(
    ParseInlines("This is a <Bold>message</Bold> with bold formatted text"));
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Yes! This is perfect! Thank you very much! – user2272143 Nov 27 '18 at 23:21
  • Your example work perfectly with normal TextBlock however I use this implementation and I'm afraid that it don't have Inlines. https://stackoverflow.com/a/49636033/2272143 Do you have any idea to how I can add inlines to this code? – user2272143 Nov 27 '18 at 23:50
  • Well, that implementation doesn't understand anything about the WPF document features, i.e. the things in System.Windows.Documents. You can't use it for text that is formatted as shown in your question. I have no idea how to easily add this feature. – Clemens Nov 28 '18 at 00:01
  • @user2272143 I'd approach it from the other way around. Add the outlining to this solution. The outline is just going and converting the text to a path. Inlines can host paths. So before you call the ParseInlines on your text, you'll need to convert the blocks of text you want outlined into paths and inject those into the flow document. – SledgeHammer Nov 28 '18 at 04:03
  • @SledgeHammer Thank you for your suggestion... I have though to one solution of that kind but in this way I have the path that don't match the text when a part of the text have a different format. Now I'm working on another example https://siderite.blogspot.com/2016/03/how-to-draw-outlined-text-in-wpf-and.html#at2665784896 and I have modified it to iterate the Inlines but I have some problems to alligne path and text. However since this is a different problem, tomorrow I will open another question. Thanks for your help! – user2272143 Nov 28 '18 at 04:28
  • I have posted a new question about the problem with outline text here https://stackoverflow.com/q/53522632/2272143 – user2272143 Nov 28 '18 at 15:25
1

TextBlock wouldn't support that directly, you'd have write a method to parse the string yourself and set the styles on the inlines. Doesn't look that difficult to parse. Use regular expressions or a token parser. Depends on how many different styles you need to support, but Regex is the easier approach.

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
  • this is my suggestion as well – hcham1 Nov 27 '18 at 22:48
  • Thanks for your answer. I can receive the string with every format style, it include also italic, underline, font size, font name, color and so on, I have no idea how I can parse all that stuff. I hope there is some parser that can do this work without expend days reinventing the wheel. – user2272143 Nov 27 '18 at 22:50
  • @user2272143 are these html tags or something standard like that? – SledgeHammer Nov 27 '18 at 22:53
  • I can get in different standards xaml, html ... when I get xaml format and I past the string in the xaml file inside it is correctly formatted. the problem is that I need to do this code behind. – user2272143 Nov 27 '18 at 22:57
  • @user2272143 Use a RichTextBox and parse a FlowDocument as shown here: stackoverflow.com/a/27957828/1136211. No need to do any parsing yourself. – Clemens Nov 27 '18 at 23:08
  • @Clemens Thanks. I check it, but I'm not sure I can use a RichTextBox because now I use a customization of a TextBlock in order to be able to add outline text effect. I have to check if I can do it with a RichTextBox. If there are no other way to do it with TextBlock I'll try to do it with RichTextBox but I hope to find other solution with TextBlock since I already expend 2 days only to find a solution for outlined text. – user2272143 Nov 27 '18 at 23:10
  • @user2272143 XamlReader may also parse a TextBlock. You could then just copy the Inlines collection. – Clemens Nov 27 '18 at 23:14
  • @Clemens Thank you very much. This can be the right solution! I try it. If you can provide me a small example on how to do it is very appreciated. – user2272143 Nov 27 '18 at 23:18