Problem:
I’m trying to access a named Run element within a FlowDocument which is defined in the Window Resources. To clarify what I mean, consider the follow code:
<Window.Resources>
<FlowDocument x:Key="doc">
<Paragraph>
<Run x:Name="run" />
</Paragraph>
</FlowDocument>
</Window.Resources>
Here, I’d be trying to access the Run element named “run.”
What I’ve Tried So Far:
-
Simply using the element’s name to access it. However, named elements in the window’s resources apparently don’t have the same default accessibility as elements defined in the window’s content, because this method did not work.
-
Attempting to add a key to the
Runelement and then accessing the element through theFindResource()method. Unfortunately, it seems that keys cannot be added to nested elements. -
The following code, which throws a
NullReferenceException:FlowDocument doc = (FlowDocument)FindResource("doc"); ((Run)doc.FindName("run")).Text = "example text";
You can use
LogicalTreeHelper.FindLogicalNodeasRemarks from above link: