Make a Part of Text Bold inside TextBlock

Khushi

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?

Thomas Weller

If you want to display HTML, use a Webbrowser control.

<WebBrowser Name="myWebBrowser"/>

And in your code, pass your text like this:

myWebBrowser.NavigateToString(myHTMLString);

If not, and bold is the only thing to be done and cannot be nested, you can do it like this:

string s = "<b>This</b> is <b>bold</b> text <b>bold</b> again."; // Sample text
var parts = s.Split(new []{"<b>", "</b>"}, StringSplitOptions.None);
bool isbold = false; // Start in normal mode
foreach (var part in parts)
{
     if (isbold)
        myTextBlock.Inlines.Add(new Bold(new Run(part)));
     else
        myTextBlock.Inlines.Add(new Run(part));

     isbold = !isbold; // toggle between bold and not bold
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Make a Part of Text Bold inside TextBlock

From Dev

Can i dynamically make part of text in textblock bold?

From Dev

make part of the text bold in a UITextField

From Dev

How to make a part of text bold from arraylist in android

From Dev

Make text bold in vim

From Dev

Make bold specific part of string

From Dev

Make part of a UILabel bold in Swift

From Dev

Make bold specific part of string

From Dev

Center and bold part of a text in Swift

From Dev

How to bold part of the text in dbgrid?

From Dev

Make the text enclosed in bold tag as bold

From Dev

Make the text enclosed in bold tag as bold

From Dev

How to make some part of the text label bold in iOS Objective C programmatically?

From Dev

Make text between asterisks bold

From Dev

How to make button text bold?

From Dev

iOS - UITableViewCell make text bold

From Dev

Make text bold in image of book

From Dev

Make TextBlock Bold only if certain condition is true, via Binding

From Dev

WPF C# - Get inline formated bold Text from TextBlock

From Dev

WPF C# - Get inline formated bold Text from TextBlock

From Dev

Make part of a matplotlib title bold and a different color

From Dev

How to make part of a string bold in iOS?

From Dev

Is there a unicode way to make part of a string bold?

From Dev

How to make a part of string variable bold

From Dev

Highlight part of text in textblock not working on all listboxItems

From Dev

how to bold a part of text Android String

From Dev

How to Bold certain text part of a string

From Dev

Access TextBlock.Text Inside a Button

From Dev

Convert Enum to string inside TextBlock text