Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bold part of text string code behind

Tags:

c#

xaml

We built an application in XAML. Now I need to format the text strings. Is it possible to bold only part of the text string. We are replacing an element's text in a XAML textblock with a text string. What would be the easiest way to make parts of the text string bold? Would I add a label? Something else in the XAML or in the C#?

Here is an example of our XAML and our code behind:

XAML

<TextBlock x:Name="PrimaryNameText" Text="Primary Member Name:"></TextBlock>

C# String

PrimaryNameText.Text = "Primary Member Name: " + reAccount.MyPerson.Prefix + " " + reAccount.MyPerson.FirstName + " " + reAccount.MyPerson.LastName;

In the example above, we more or less want to bold the part of the string "Primary Member Name: "

I know you can do a "\n" for a page break, is there a way we can do something for bolding text in the string?

like image 335
ClosDesign Avatar asked Jan 17 '26 05:01

ClosDesign


1 Answers

Use a span inside your TextBlock.

<TextBlock> 
    <Span x:Name="PrimaryNameBold" FontWeight="Bold"></Span> 
    <Span x:Name="PrimaryNameNormal"></Span> 
</TextBlock> 

And in your code:

PrimaryNameBold.Text = "Primary Member Name: ";
PrimaryNameNormal.Text = reAccount.MyPerson.Prefix + " " + reAccount.MyPerson.FirstName + " " + reAccount.MyPerson.LastName;

Not an awesome solution, but this should work.

like image 133
tnw Avatar answered Jan 19 '26 19:01

tnw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!