Apply a style to Run objects of a generated TextBlock
I have a generated TextBlock with two Run objects inside. I would like to assign a color to the first Run object and another color to the other one, and I'd like to specify this in XAML.
this is the codebehind:
TextBlock tb = new TextBlock();
tb.Style = Application.Current.Resources["开发者_Python百科MyTextStyle"] as Style;
tb.Inlines.Add(new Run { Text = "Line 1" });
tb.Inlines.Add(new LineBreak());
tb.Inlines.Add(new Run { Text = "Line 2" });
container.Child = tb;
and this is the XAML:
<Style x:Key="MyTextStyle" TargetType="TextBlock">
<Setter Property="TextWrapping" Value="NoWrap" />
<Setter Property="Foreground" Value="{StaticResource PhoneAccentColor}" />
</Style>
how should I modify the XAML code?
thank you.
Perhaps this way?
<TextBlock Style="{StaticResource Comment}">
<Run FontWeight="Bold" Foreground="Red">line1</Run><LineBreak/><Run>line2</Run>
</TextBlock>
精彩评论