Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a Part of Text Bold inside TextBlock

Tags:

c#

wpf

xaml

I know that we can use <Run> in XAML to achieve what I am asking :

<TextBlock.Inlines>
    <Run Text="This is" />
    <Run FontWeight="Bold" Text="Bold Text." />
</TextBlock.Inlines>

Also I can do it in code behind as follows:

TextBlock.Inlines.Add(new Run("This is"));
TextBlock.Inlines.Add(new Bold(new Run("Bold Text.")));

But my problem is something different:

Suppose I have following Text in my database:

This is <b>Bold Text</b>.

Now, my Textblock is bound to a field that contains the above text in database.

I want the text between <b> and </b> to be bold. How can I achieve this?

like image 772
Khushi Avatar asked Dec 28 '13 21:12

Khushi


People also ask

Can we make the text bold within the text box?

With the cursor on top of the text box, right click, then select Font and then Font Style "Bold".

How do you make a string bold in C#?

Wrap a word in string with '<b>' to make it Bold without changing word's original case.

How do I make text bold in WPF?

textBlock. Inlines. Add(new Run(Boldsplist) { FontWeight = FontWeights. Bold }); this.

Is TextBlock editable?

TextBlock is not editable.


2 Answers

It looks like you want to replace your custom formatting with <Bold> - see TextBlock for more info. Sample from the article:

<TextBlock Name="textBlock1" TextWrapping="Wrap">
  <Bold>TextBlock</Bold> is designed to be <Italic>lightweight</Italic>,
  and is geared specifically at integrating <Italic>small</Italic> portions
  of flow content into a UI.
</TextBlock>

One approach is to re-format string to match what TextBlock expects.

If you have HTML input - parse the text with HtmlAgilityPack first and than walk though resulting elements and construct string with b-elements replaced with text wrapped <Bold> and similar to other formatting.

If database content is known to have only valid begin/end pairs (not random HTML) you may even get away with basic String.Replace : text = text.Replace( "", "")`.

If you have you own custom formatting (like *boldtext*) you'll need to invent custom parser for that.

like image 142
Alexei Levenkov Avatar answered Oct 02 '22 03:10

Alexei Levenkov


You can subscribe to TargetUpdated event:

 void textBlock_TargetUpdated(object sender, DataTransferEventArgs e)
 {
        string text = textBlock.Text;

        if (text.Contains("<b>"))
        {
            textBlock.Text = "";
            int startIndex = text.IndexOf("<b>");
            int endIndex = text.IndexOf("</b>");
            textBlock.Inlines.Add(new Run(text.Substring(0, startIndex)));
            textBlock.Inlines.Add(new Bold(new Run(text.Substring(startIndex + 3, endIndex - (startIndex + 3)))));
            textBlock.Inlines.Add(new Run(text.Substring(endIndex + 4)));
        }
    }

and XAML for the TextBlock:

<TextBlock x:Name="textBlock" Text="{Binding NotifyOnTargetUpdated=True}"></TextBlock>
like image 24
w.b Avatar answered Oct 02 '22 05:10

w.b