简体   繁体   English

文本框的背景色部分

[英]Background color part of textbox

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. 由于在TextBlock本身中没有此功能,因此我认为应该添加内联代码。

However the inline objects (Run, Span, eg) have a private background field that gets its value from the parent TextBlock. 但是,内联对象(例如Run,Span)具有私有背景字段,该背景字段从父TextBlock获取其值。

I figured I'd try to use an InlineUIContainer. 我认为我会尝试使用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. 最后一行给出ArgumentException。 Is there a way to change a part of a TextBlocks background color? 有没有办法更改TextBlocks背景颜色的一部分? If so, am I on the right track? 如果是这样,那我走对了吗?

May be following option can helps you. 可能是以下选项可以帮助您。

You can use RitchTextBox , since in WindowsPhone RitchTextBox is read only (as an TextBlock). 您可以使用RitchTextBox ,因为在WindowsPhone中RitchTextBox是只读的(作为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 UPD#1不要忘记内联TextBlocks的TextWrapping

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM