开发者

WPF RichTextBox: Replace text with UI controls @ run time

I need to develop a messenger-like text box where ce开发者_StackOverflow中文版rtain tokens are being replaced with UI controls. Just for example, if the user types :-), it should be replaced with a smiley image.

I have previous experience with WPF's RichTextBox, and I understand the concept of TextPointer and TextContent.

I just want to know: how can I replace a TextRange with a UI control?


Just figured out how to do it :-) Enjoy!

    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;

        }
    }


Here is another option (the syntax formatter is mine, implement your own according to your syntax):

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


Sorry, forgot the method's last section: :-)

... ...

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


    }

Have fun!, leave your comments if you have ones.

-Gili

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜