Extending FlowDocument with custom text elements in wpf

I would like to extend the functionality of FlowDocument by creating my own derivatives of Span and Run.

Is this possible, and did anyone find any examples?

+6
wpf
source share
2 answers

Microsoft has not disclosed the methods required to create a custom TextElement inside one of its document classes. If you look at the code for Run or Span there is no actual rendering code. Clearance takes place in a UIElement created by various inner classes (for example, FixedTextBuilder).

From the MSDN notes on FrameworkContentElement :

FrameworkContentElement does not yet define its own rendering behavior; An instance of the actual instance of the FrameworkContentElement class in code or markup is possible, but nothing is displayed in the user interface of the WPF application. Visualization logic must be provided by classes that accept FrameworkContentElement children as part of their content model or in derived FrameworkContentElement classes.

Everything is not lost, support for creating custom Block and Inline elements is provided through BlockUIContainer and InlineUIContainer . You can then create a low-level UIElement that will be placed inside a fixed or streaming document, or use higher-level WPF elements.

+1
source share

What extensibility is needed? Usually, when it comes to changing the default behavior of any WPF control that cannot be changed using inheritance, we use Attached Properties .

Here is an example of a Span element associated with a set of elements. The same trick with attached properties helps bind Run to text.

The answer will also depend on the context of the use of the FlowDocument . If it is in read-only mode, BlockUIContainer and InlineUIContainer are your friends. But if the FlowDocument is inside the RichTextBox , you will hate and curse them (copy / paste problems, undo / redo, etc.) along with the entire WPF text support infrastructure.

+1
source share

All Articles