我一直在尝试为文本块的一部分设置背景颜色。由于TextBlock本身没有这个功能,我想我应该添加内联。
然而,内联对象(Run,Span,例如)有一个私有背景字段,它从父TextBlock中获取它的值。
我想我会尝试使用InlineUIContainer。这是我使用的代码:
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);
最后一行给出了ArgumentException。有没有办法改变TextBlocks背景颜色的一部分?如果是这样,我是否走在正确的轨道上?
答案 0 :(得分:1)
可能是以下选项可以帮助您。
您可以使用 RitchTextBox ,因为在WindowsPhone中RitchTextBox是只读的(作为TextBlock)。 我的快速示例:):
<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 不要忘记内联TextBlocks的TextWrapping