簡體   English   中英

高亮顯示文本richtextblock UWP

[英]Highlight text richtextblock UWP

我想在保持相同格式的同時突出顯示文本:粗體,下划線,斜體...但是我只能通過丟失格式來突出顯示文本。 到達我想要的位置是否正確,或者是否有另一種方式可以突出顯示文本而不必“拆分”然后重新組合文本?

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-突出顯示一個或多個文本范圍。

但是如何使用它們? 請幫幫我..!

要將TextHighlighterRichTextBlock ,您可以首先創建所需的TextRange ,使用它們初始化TextHighlighter ,然后使用TextHighlighters屬性將它們應用於RichTextBlock

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

有沒有一個簡單的屬性或方法在暴露RichTextBlock控制您的要求來查找文本RichTextBlock不失格式,你應該在中設置的每個文本屬性RichTextBlock讓他們再有格式。 作為一個簡單的場景,您也可以僅通過Run對象過濾文本,然后配置所有文本的格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM