突出显示文本richtextblock UWP

时间:2018-01-26 12:02:01

标签: c# uwp highlight

我想在保持相同格式的同时突出显示文本:粗体,下划线,斜体......但我只是设法通过丢失格式来突出显示文本。达到我想要的是否正确或是否有另一种方法来突出显示文本而不必“拆分”它然后重新组装它?

XAML:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="300">
        <RichTextBlock x:Name="RichTextBlockText">
            <Paragraph x:Name="Testo">
                <Run Foreground="Gray" FontFamily="Segoe UI Light" FontSize="24">
                    This is a
                </Run>
                <Run Foreground="Teal" FontFamily="Georgia" FontSize="18" FontStyle="Italic">
                    different text
                </Run>
                <Run Foreground="Black" FontFamily="Arial" FontSize="14" FontWeight="Bold">
                    format
                </Run>
            </Paragraph>
        </RichTextBlock>
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="0,25,0,0">
            <TextBox x:Name="txbToFind" Height="32" VerticalAlignment="Bottom" Width="200" HorizontalAlignment="Left"/>
            <Button x:Name="btnFind" Content="Find" Click="btnFind_Click" HorizontalAlignment="Right" VerticalAlignment="Center"/>
        </Grid>
    </StackPanel>
</Grid>

xaml.cs:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void btnFind_Click(object sender, RoutedEventArgs e)
    {
        string text = string.Empty;
        TextBlock NewText = new TextBlock();
        string toFind = txbToFind.Text;

        for (int a = 0; a <= Testo.Inlines.Count - 1; a++)
        {
            Run runCorrente = Testo.Inlines[a] as Run;
            string currentText;
            currentText = runCorrente.Text;
            text += currentText;
        }

        if (text.IndexOf(toFind) >= 0)
        {
            string[] partOfText = text.Split(new String[] { toFind }, StringSplitOptions.None);
            Paragraph paragraph = new Paragraph();
            for (int a = 0; a <= partOfText.Length - 1; a++)
            {
                var piece = partOfText[a];
                Run run = new Run();
                run.Text = piece;
                paragraph.Inlines.Add(run);
                if (a < partOfText.Length - 1)
                {
                    MakeHighlightedParagraph(paragraph, toFind, RichTextBlockText);
                }
            }
            RichTextBlockText.Blocks.Clear();
            RichTextBlockText.Blocks.Add(paragraph);
        }
    }

    private void MakeHighlightedParagraph(Paragraph paragraph, string textToHighlight, RichTextBlock textBlock)
    {
        InlineUIContainer cont = new InlineUIContainer();
        var border = new Border();
        border.MinWidth = textBlock.FontSize / 3.5;
        border.Background = new SolidColorBrush(Colors.Yellow);
        var text = new TextBlock();
        text.Text = textToHighlight;
        var margin = textBlock.FontSize * (3.0 / 14.0) + 1.0;
        text.Margin = new Thickness(0.0, 0.0, 0.0, -margin);
        border.Child = text;
        cont.Child = border;
        paragraph.Inlines.Add(cont);
    }
}

提前致谢...!

我找到了一些可能的解决方案,但我不能使用它们:

TextRange - 选择文本的一部分; TextHighlighter - 突出显示一个或多个文本范围。

但它们是如何使用的?请帮帮我..!

2 个答案:

答案 0 :(得分:1)

要将TextHighlighterRichTextBlock一起使用,您可以先创建所需的TextRange,然后使用它们初始化TextHighlighter,然后将其应用到RichTextBlock使用TextHighlighters属性:

TextRange textRange = new TextRange() { StartIndex = 3, Length = 10 };            
TextHighlighter highlighter = new TextHighlighter()
{
    Background = new SolidColorBrush(Colors.Yellow),
    Ranges = { textRange }
};
//add the highlighter
RichBlock.TextHighlighters.Add(highlighter);

答案 1 :(得分:0)

#preview控件中没有显示简单的属性或方法,因为您需要在RichTextBlock中查找文本而不会丢失格式,您应该在每个文本中设置属性RichTextBlock让他们再次拥有格式。作为一个简单的场景,您也可以只通过RichTextBlock对象过滤文本,然后配置所有文本的格式。