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 my开发者_运维问答Text}" />
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
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);
精彩评论