【问题标题】:TextHighlighter RichTextBlock UWPTextHighlighter RichTextBlock UWP
【发布时间】:2018-07-06 17:29:03
【问题描述】:

我想用TextHighlighter 对象突出显示富文本块的一些文本。 我创建了一个TextRange,将它添加到一个列表中,然后我创建了一个TextHighlighter 的新实例并设置了背景颜色。但现在我不能使用TextHighlighter 来突出显示文本。我应该如何进行?

xaml:

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

xaml.cs:

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

    private void btnToFind_Click(object sender, RoutedEventArgs e)
    {
        TextRange textRange = new TextRange() { StartIndex = 3, Length = 5 };
        List<TextRange> rangelist = new List<TextRange>();
        rangelist.Add(textRange);
        TextHighlighter evidenziatore = new TextHighlighter() { Background = new SolidColorBrush(Colors.Yellow) };
        //RichFullText... There I would apply highlight to RichTextBlock
    }
}

如何使用TextHighlighter 突出显示?

【问题讨论】:

    标签: c# uwp highlight


    【解决方案1】:

    您需要将TextHighlighter 添加到RichTextBlockTextHighlighters 集合中:

    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);
    

    另请注意,我已将textRange 添加到您的TextHighlighter 实例中的Ranges 集合中。

    【讨论】:

    • 如果这解决了您的问题,请考虑接受答案以便问题得到解决:-)。您可以通过单击灰色刻度线来完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 2020-04-23
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多