WPF RichTextBox:用UI控件@运行时替换文本

时间:2010-07-03 13:58:18

标签: wpf richtextbox

我需要开发一个类似信使的文本框,其中某些标记正在被UI控件替换。 例如,如果用户键入:-),则应将其替换为笑脸图片。

我之前有过使用WPF RichTextBox的经验,我理解TextPointerTextContent的概念。

我只想知道:如何用UI控件替换TextRange

3 个答案:

答案 0 :(得分:4)

想出怎么做:-) 享受!

    public static void ReplaceTextRangeWithUIControl(this RichTextBox textBox, TextRange textRange)
    {                                   
        if (textRange.Start.Parent is Run)
        {
            var run = textRange.Start.Parent as Run;

            var runBefore =
                new Run(new TextRange(run.ContentStart,textRange.Start).Text);
            var runAfter =
                new Run(new TextRange(textRange.End,run.ContentEnd).Text);


            textRange.Start.Paragraph.Inlines.Add(runBefore);
            textRange.Start.Paragraph.Inlines.Add(new TextBlock() { Background = Brushes.Green, Text = textRange.Text });
            textRange.Start.Paragraph.Inlines.Add(runAfter);
            textRange.Start.Paragraph.Inlines.Remove(run);

            textBox.CaretPosition = runAfter.ContentEnd;

        }
    }

答案 1 :(得分:1)

这是另一个选项(语法格式化程序是我的,根据你的语法实现自己的):

    private void ReplaceTokensWithControl(Run run)
    {
        var text = run.Text;

        bool inToken = false;
        var startIndex = 0;
        var endIndex = 0;
        for (var i = 0; i < text.Length; i++)
        {
            if (Char.IsWhiteSpace(text[i]) | SyntaxFormatter.TextControlSpecialTokens.Contains(text[i]))
            {
                if (i > 0 && !(Char.IsWhiteSpace(text[i - 1]) | SyntaxFormatter.TextControlSpecialTokens.Contains(text[i - 1])))
                {
                    endIndex = i - 1;
                    string token = text.Substring(startIndex, endIndex - startIndex + 1);

                    string tokenContext = text.Substring(0, startIndex);
                    if (SyntaxFormatter.IsTextControlToken(token, tokenContext))
                    {
                        var textBefore = run.Text.Substring(0, startIndex);
                        var runBefore = new Run(textBefore);
                        run.ContentStart.Paragraph.Inlines.InsertBefore(run, runBefore);

                        Run runAfter = null;
                        if (endIndex + 1 < run.Text.Length)
                        {
                            var textAfter = run.Text.Substring(endIndex + 1, run.Text.Length - (endIndex + 1));
                            runAfter = new Run(textAfter);
                            run.ContentStart.Paragraph.Inlines.InsertAfter(runBefore, runAfter);
                        }

                        runBefore.ContentStart
                            .Paragraph
                            .Inlines
                            .InsertAfter(runBefore,new InlineUIContainer(SyntaxFormatter.GetTokenTextControl(text, tokenContext)));

                        run.ContentStart.Paragraph.Inlines.Remove(run);                                                

                        if (runAfter != null)
                            ReplaceTokensWithControl(runAfter);

                        return;
                    }
                }
            }
            else
            {
                if (!inToken)
                {
                    inToken = true;
                    startIndex = i;
                }
            }
        }

答案 2 :(得分:1)

对不起,忘了方法的最后一节:: - )

... ...

        var lastWord = text.Substring(startIndex, text.Length - startIndex);
        if (SyntaxFormatter.IsTextToken(lastWord))
        {
            var tag = new SyntaxTokenProperties();
            tag.StartPosition = run.ContentStart.GetPositionAtOffset(startIndex, LogicalDirection.Forward);
            tag.EndPosition = run.ContentStart.GetPositionAtOffset(endIndex + 1, LogicalDirection.Backward);
            tag.Word = lastWord;                
        }


    }

玩得开心!如果你有评论,请留下你的意见。

-Gili