This is working in WPF documents, but I am thinking this is not restricted to that scope and applies at least to all WPF custom controls. I have a custom, composite, inline control in a WPF document. My control inherits from Span and has a custom Run control as a child.
Public Class MyTextInput
Inherits Span
Private mInnerRun As BindableRun
When attempting to highlight text that is found in a search of the flowdocument, I am setting the background property of the TextRange that defines the found string using the ApplyPropertyValue() method. Like so:
currentRange.ApplyPropertyValue(Inline.BackgroundProperty, SystemColors.HighlightBrush)
For normal document content this works fine, however, when the found text is inside of my custom control the call fails with this ArgumentException:
Cannot set Expression. It is marked as 'NonShareable' and has already been used.
Interestingly, if the BindableRun element is stand-alone in the FlowDocument, not present as part of the MyTextInput element, the call succeeds no problem. This leads me to think that I need to set some attribute or similar on the composite control, or its children, to expose access to the child controls properties. Anyone dealt with this before?? Thanks
Edit 1/2/2009:
More thoughts on this: seeing as the direct access of the Run.Background property works, I am thinking of what has to happen to set the background of an arbitrary range of text in a FlowDocument. The range will be a subset of, or maybe span multiple InLine elements. So, take the following XAML:
<FlowDocument>
…
<Run> I really admire the Governor of Illinois, he seems so clever!</Run>
…
</FlowDocument>
Now, using the background color method, as shown above in original question, set the background for the TextRange that defines “admire”. What you end up with is:
<FlowDocument>
…
<Run> I really</Run><Run Background=”#FFFFFF00”> admire</Run><Run> the Governor of Illinois, he seems so clever!</Run>
…
</FlowDocument>
This makes perfect sense, since the only way to style that word alone is to have it in a separate InLine element. So, behind the scenes, The original run is split into 3 runs, and the target run is set with the correct background. When you remove the Highlight after the user changes focus on the document, it cleans up the extra runs and combines it all back into the original run.
So, with that thought, I am thinking where I am getting an error is that my derived Span is not playing well with the dynamic shuffling of its children. So, the original question still stands, any way to enable this?