How to make the text bold of TextBlock in Silverlight?
Asked Answered
P

2

9

I am developing window phone 7 application in C#. I am new to the window phone 7 application. I am also new to the silverlight. I want to generate the bold text of Texblock dynamically. I want to generate the bold text only for some part of the text. I am using the following code

IncometextBlock.Text = "Income entries on " + selectedDate.ToShortDateString() + "        Page - "+SelectedButtonName+"";

I want the output as

"Income entries on 21/01/2011 Page - A"

I want the above output. How to make the bold text for above requirement ? Can you please provide me any code or link through which I can resolve the above issue. If I am doing anything wrong then please guide me.

Pytlik answered 21/2, 2011 at 9:25 Comment(0)
S
24

I'd do it like this.

IncometextBlock.Inlines.Clear();
IncometextBlock.Inlines.Add(new Run() {Text = "Income entries", FontWeight = FontWeights.Bold});
IncometextBlock.Inlines.Add(new Run() {Text = " on " }); 
IncometextBlock.Inlines.Add(new Run() {Text = selectedDate.ToShortDateString(), FontWeight = FontWeights.Bold});
IncometextBlock.Inlines.Add(new Run() {Text = "     Page - "}); 
IncometextBlock.Inlines.Add(new Run() {Text = SelectedButtonName, FontWeight = FontWeights.Bold});
Sexennial answered 21/2, 2011 at 13:23 Comment(1)
What if your data was databound?Plus
T
7

If you use the WrapPanel (from the Toolkit) you can do this:

<Grid>
    <toolkit:WrapPanel>
        <TextBlock Text="Income entries" FontWeight="Bold"/>
        <TextBlock Text=" on "/>
        <TextBlock Text="21/01/2011" FontWeight="Bold"/>
        <TextBlock Text=" Page - "/>
        <TextBlock Text="A" FontWeight="Bold"/>
    </toolkit:WrapPanel>
</Grid>

(The above is only in a grid to enable code highlighting here in SO and does not need to be for the effect to work.)

Trichomoniasis answered 21/2, 2011 at 10:9 Comment(1)
WP7 supports Run text elements in a text block and text block can handle text wrapping far more efficiently. Bringing in the toolkit heavy guns to shoot this little gnat of a problem is a little OTT, IMO.Sexennial

© 2022 - 2024 — McMap. All rights reserved.