Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 突出显示文本richtextblock UWP_C#_Uwp_Highlight - Fatal编程技术网

C# 突出显示文本richtextblock UWP

C# 突出显示文本richtextblock UWP,c#,uwp,highlight,C#,Uwp,Highlight,我想突出显示文本,同时保持相同的格式:粗体、下划线、斜体。。。但我只是通过丢失格式来突出显示文本。达到我想要的是正确的吗?或者有没有其他方法可以突出显示文本,而不必“拆分”文本,然后重新组装 xaml: 这是一个 不同文本 格式 xaml.cs: public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } pri

我想突出显示文本,同时保持相同的格式:粗体、下划线、斜体。。。但我只是通过丢失格式来突出显示文本。达到我想要的是正确的吗?或者有没有其他方法可以突出显示文本,而不必“拆分”文本,然后重新组装

xaml:


这是一个
不同文本
格式
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);
    }
}
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
私有void btnFind\u单击(对象发送者,路由目标)
{
string text=string.Empty;
TextBlock NewText=新TextBlock();
字符串toFind=txbToFind.Text;
对于(int a=0;a=0)
{
string[]partOfText=text.Split(新字符串[]{toFind},StringSplitOptions.None);
段落=新段落();

对于(int a=0;a在
RichTextBlock
控件中没有一个简单的属性或方法可以满足您在
RichTextBlock
中查找文本而不丢失格式的要求,您应该在
RichTextBlock
中的每个文本上设置属性,使它们重新具有格式。作为一个简单的scenario,您也可以通过
Run
对象过滤文本,然后配置所有文本的格式。

RichTextBlock
控件中没有一个简单的属性或方法可以满足您在
RichTextBlock
中查找文本而不丢失格式的要求,您应该在eve上设置属性在
RichTextBlock
中过滤文本,使其重新具有格式。作为一个简单的场景,您也可以通过
Run
对象过滤文本,然后配置所有文本的格式。

要将
TextHighlighter
RichTextBlock
一起使用,您可以首先创建所需的
TextRange
s,初始化
TextHighlighter
s使用它们,然后将它们应用到
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);

要将
TextHighlighter
s与
RichTextBlock
一起使用,您可以首先创建所需的
TextRange
s,使用它们初始化
TextHighlighter
s,然后使用
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);