// Inserts text run into TextBlock in a form consistent with flow schema requirements. // // TextBlock has dual role as a text container - // plain text for TextBox and rich inline-collection content for everything else. // In case of plain text we insert only string into the text container, // in all other cases we must wrap eact rext run by Run inline element. private static void InsertTextRun(ITextPointer position, string text, bool whitespacesIgnorable) { // We distinguish these two cases by parent of TextContainer: // for plain text case it is TextBox. if (!(position is TextPointer) || ((TextPointer)position).Parent == null || ((TextPointer)position).Parent is TextBox) { position.InsertTextInRun(text); } else { if (!whitespacesIgnorable || text.Trim().Length > 0) { Run implicitRun = Inline.CreateImplicitRun(((TextPointer)position).Parent); ((TextPointer)position).InsertTextElement(implicitRun); implicitRun.Text = text; } } }