开发者

Apply a style to Run objects of a generated TextBlock

开发者 https://www.devze.com 2023-03-09 17:54 出处:网络
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.

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消