Apparently e.Source
is the Document
of the RichTextBox that you clicked on. It is a FlowDocument
, which is not derived from UIElement
.
You may however access the RichTextBox by the FlowDocument's Parent
property.
if (e.Source is FlowDocument)
{
selectedElement = ((FlowDocument)e.Source).Parent as UIElement;
}
else
{
selectedElement = e.Source as UIElement;
}