Inserting Text without using the clipboard

May 28, 2012 at 1:25 AM

I've also been playing around with the Blend APIs and was disappointed with the lack of anything to write text. However, I've found a much better solution than using the clipboard.

ITextEditor doesn't have anything, but CodeEditor (from memory that's what it's called) does. CodeEditor is the implementation of ITextEditor that Blend gives you. It's a non-public class so it makes things a bit more difficult, but it has a Property called TextBuffer. This is the same TextBuffer used in the Visual Studio API. You can get it like this...

        Microsoft.VisualStudio.Text.ITextBuffer GetTextBuffer()
        {
            return (Microsoft.VisualStudio.Text.ITextBuffer)m_textEditor.GetType().GetProperty("TextBuffer").GetValue(m_textEditor, null);
        }

You will need to reference Microsoft.VisualStudio.Platform which is in the blend folder.

There's the obvious risk that there is no guarantee the object you get has a TextBuffer property, but I think it's the best solution.

Note: Although I would have thought you could say ((dynamic)m_textEditor).TextBuffer; It didn't work for me.