I would like to determine if there is an InlineUIContainer (or BlockUIContainer) in the current Caret position in the WPF RichTextBox.
I currently have a RichTextBox as shown below:
<RichTextBox SelectionChanged="RichTextBox_SelectionChanged">
<FlowDocument>
<Paragraph>
<Run>Some text before</Run>
<InlineUIContainer>
<Label>I am a label</Label>
</InlineUIContainer>
<Run>Some text after</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
In the SelectionChanged event, I tried to use;
rtf.CaretPosition.GetAdjacentElement(rtf.CaretPosition.LogicalDirection)
... which returns null.
I can do this using the MouseDoubleClicked event handler as follows:
Point pos = e.GetPosition(rtf);
TextPointer pointer = rtf.GetPositionFromPoint(pos, false);
Console.WriteLine(pointer.GetAdjacentElement(pointer.LogicalDirection));
But I would really like it to work when the position of the RichTextBox carriage changes.
Is there any way to achieve this?
Thank you in advance
Matt
source
share