brainwash
brainwash

Reputation: 689

Formatted text inside TextBlock / TextBox - With binding

This might have been asked before but I'm really looking for a simple way to display a programatically formatted text inside some text container. For decoupling purposes I would like to use a binding property, this is how I do it now:

<TextBlock Height="219" Name="_txtBox" Text="{Binding myText}" />

and then

myText = "<TextBlock>\n <Run FontWeight=\"Bold\">\n" + item1 + "\n</Run>\n " + item2 + "\n</TextBlock>";

For some unknown WP7 design reasons this does not work, I've tried with TextBox also. Is there any way that I can output some small formatted text to any kind control without over-complicating?

I would like to keep binding in place

Upvotes: 3

Views: 1911

Answers (1)

Greg Zimmers
Greg Zimmers

Reputation: 1400

This should work for you. Although I was not sure where you wanted the line breaks. You can add or remove them from the inlines collection.

XAML

<TextBlock x:Name="text1" ></TextBlock>

Code

    InlineCollection inlines = text1.Inlines;
    Run r = new Run();
    r.Text = "item 1";
    r.FontWeight = FontWeights.Bold;
    inlines.Add(r);
    inlines.Add(new LineBreak());
    r = new Run();
    r.Text = "item 2";
    inlines.Add(r);

Upvotes: 4

Related Questions