RichTextBox crashed when inserting a UIElement into it

When I insert a UIElement into a RichTextBox (e.g. Button in BlockUIContainer ). I always got an InvalidOperationException saying "This text is text without text element" when editing a richtextbox (especially select elements including UIElements).

Exception Details:

System.Windows.Documents.TextPointer.MoveToElementEdge (ElementEdge element region) System.Windows.Documents.TextPointer.System.Windows.Documents.ITextPointer.MoveToElementEdge (ElementEdge element region) System.Windows.Documents.TextPointerBase.GetFollowingNonMergeableInlineContentStart (ITextPointer position) System .Windows.Documents.TextSelection.get_PropertyPosition () System.Windows.Documents.TextSelection.GetCurrentValue (DependencyProperty formatting property)

"This text navigator" does not have a text element.

I ducked into it and found that an exception was thrown here:

 internal void MoveToElementEdge(ElementEdge edge) { ValidationHelper.VerifyElementEdge(edge, "edge"); this.VerifyNotFrozen(); this._tree.EmptyDeadPositionList(); this.SyncToTreeGeneration(); TextTreeTextElementNode scopingNode = this.GetScopingNode() as TextTreeTextElementNode; if (scopingNode == null) { throw new InvalidOperationException(SR.Get("NoScopingElement", new object[] { "This TextNavigator" })); } this.MoveToNode(this._tree, scopingNode, edge); } 

The problem seems to be related to TextTreeTextElementNode. But I can no longer trace, because it is too difficult for me. I think UIElement cannot be wrapped with this "TextTreeTextElementNode" and this will return null.

I tried to do a google search but did not find any valuable information just for feedback from Microsoft.

+4
source share

Source: https://habr.com/ru/post/1397360/