【问题标题】:Background color part of textbox文本框的背景颜色部分
【发布时间】:2015-05-22 12:50:00
【问题描述】:

我一直在尝试为文本块的一部分设置背景颜色。由于 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 背景颜色的一部分?如果是这样,我是否走在正确的轨道上?

【问题讨论】:

  • 你不能使用TextBlock.Background属性吗?
  • 在 WindowsPhone TextBlock 中不包含 Background 属性,仅在 WPF 中
  • 我想改变文本块背景的一部分,而不是整个背景。基本上我的字符串很长,需要“突出显示”一个单词。所以使用文本块的背景属性不会做我想做的事,在这种情况下
  • @f14shm4n 我不知道,抱歉。

标签: c# xaml windows-phone-8.1 windows-8.1 win-universal-app


【解决方案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不要忘记内联文本块的 TextWrapping

【讨论】:

  • 我看到的是 RichTextBlock,而不是 RichTextBox。当我尝试使用它时,我收到消息:“不包含'inlines'的定义”。
  • 你可以用类似的问题阅读这个thread,但我猜 RichTextBlock 的行为就像 TextBlock(不能在 InlineCollection 中添加 InlineUIContainer)(我还没有在 WP 8.1 中工作过)添加了 RitchTextBlock)。基于此,在这种情况下只能使用一个选项,即 RitchTextBox(正如我所说,此控件的行为就像 TextBlock,因为在 Silverlight 中为 isReadOnly)。
猜你喜欢
  • 1970-01-01
  • 2018-08-27
  • 2012-12-28
  • 1970-01-01
  • 2021-03-28
  • 2022-11-29
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
相关资源
最近更新 更多