1

I get text from DB in single line. For example "Hello my name is the Developer". I need the part of this text will be in bold and put it in TextBlock.

For code I use:

overview_text.Text = overView;

Where overview_text TextBlock is TextBlock and overView is string from db.

I can alter the DB string as I wish but I need single string, for example what I need is:

"Hello my <bold> name </bold> is the Developer"

Can you please help me with this?

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
Dim
  • 4,527
  • 15
  • 80
  • 139

1 Answers1

5

There are many ways to achieve your desired result. The RichTextBox way is far from optimal and dates back to Windows Forms controls, where this was more difficult to achieve. Generally, the best class to use for this in WPF would be the Run class.

Some of the many benefits of using the Run class include the fact that its Text property is bindable and we can apply all of the usual text manipulation properties on it as you can see from the example below:

<TextBlock FontSize="16" Padding="25">
    <Run Text="Here is some " />
    <Run Text="BOLD" FontWeight="Bold" />
    <Run Text=" and " />
    <Run Text="ITALIC" FontStyle="Italic" />
    <Run Text=" and " />
    <Run Text="COLOURED" Foreground="LightGreen" />
    <Run Text=" text in one TextBlock" />
</TextBlock>

enter image description here

Sheridan
  • 68,826
  • 24
  • 143
  • 183