Reputation: 508
I've been trying to set a background color to a part of a textblock. Since there is no function to this with in the TextBlock itself I figured I should be adding inlines.
However the inline objects (Run, Span, e.g) have a private background field that gets its value from the parent TextBlock.
I figured I'd try to use an InlineUIContainer. Here is the code I used:
Grid grid = new Grid();
grid.Background = new SolidColorBrush(Colors.Red);
TextBlock block = new TextBlock();
block.Text = temp2;
grid.Children.Add(block);
InlineUIContainer iuic= new InlineUIContainer();
iuic.Child = grid;
Parent_TextBlock.Inlines.Add(iuic);
This last line gives an ArgumentException. Is there a way to change a part of a TextBlocks background color? If so, am I on the right track?
Upvotes: 0
Views: 693
Reputation: 461
May be following option can helps you.
You can use RitchTextBox, since in WindowsPhone RitchTextBox is read only (as an TextBlock). My quick sample :) :
<RichTextBox>
<Paragraph>
Some textSome textSome textSome textSome
<Span Foreground="#FF0E0E0E">textSome textSome textSome</Span>
textSome textSome textSome textSome textSome textSome textSome textSome textSome
textSome textSome textSome textSome textSome textSome textSome textSome textSome
<InlineUIContainer> <!-- Look this -->
<Border Background="Blue">
<TextBlock Text="textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome text"/>
</Border>
</InlineUIContainer>
textSome textSome textSome textSome textSome textSome textSome textSome textSome
textSome textSome textSome textSome textSome textSome textSome textSome textSome
</Paragraph>
</RichTextBox>
UPD #1 Do not forget about TextWrapping for Inline TextBlocks
Upvotes: 1