Cedric Reichenbach
Cedric Reichenbach

Reputation: 9319

Insert Span into TextBlock programmatically

I want to create the following snippet programmatically:

<TextBlock>
    12.01.2013
    <Span xml:space="preserve" Foreground="#FFCCCCCC"> 21:09</Span>
</TextBlock>

But TextBlock seems not to support TextPointers, so I cannot see a way to insert a Span.

Upvotes: 1

Views: 581

Answers (1)

Phil
Phil

Reputation: 42991

This works for me for Phone 7.1:

C#

var tb = new TextBlock();
tb.Inlines.Add("12.01.2013");

var span = new Span { Foreground = new SolidColorBrush(Colors.Red) };
span.Inlines.Add(" 21:09");
tb.Inlines.Add(span);

content.Content = tb;

Xaml

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ContentPresenter x:Name="content"/>
</Grid>

Upvotes: 3

Related Questions