Home of the AlmostImplementedException

WPF – RichTextBox: How to enable embedded Controls

There is an interesting thing about the RichTextControl most people don’t know. The Flow-Document used to store the content of a RichTextBox can handle controls. Which means you can put controls in your text and let the user interact. Maybe you want to let the user choose between different options with a checkbox or show them entire dynamic formulars.
In future posts i will show different ways to add controls to a text, but for now we just use a little easy example:

Here you see an easy way to set up a new FlowDocument inside of a RichTextBox. Its easy to see how it works, but not very comfortable in real life.
When you run this code inside a wpf-window you will see a RichTextBox with a little text and a CheckBox that says “Click me!”. But when you try …
nothing! It doesn’t work.
Thats because of microsofts way of handling things. They don’t activate the controls by default, don’t know why.
But here is the little trick to activate them. Set IsDocumentEnabled to true.

Try again and you can see it works! By the way, when you click on the checkbox the flow-document is changed to store the value but the TextChanged-Event isn’t firering!

To set the CheckBox checked right from the start, just add the property IsChecked and set it to true.

Share :

, , ,

Leave a Reply