views:

37

answers:

3

Hi Guys,

I need to develop a messenger-like text box, where certain 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 don't know how to replace a TextRange with a UI control...

Would extremely appreciate your help :-) !

Gili

A: 

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;

        }
    }
Gili
A: 

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;
                }
            }
        }
Gili
A: 

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

Gili