Question

I need some advanced validation in WPF Richtextbox for flowdocuments, something like:

a) formatting can be applied only to the whole paragraph b) no spans are allowed c) these rules also need to be applied for Text pasted from clipboard.

What is the best way to do it?

Was it helpful?

Solution

Add an event handler to the textchanged event and apply whatever formatting you need done there. The event will fire no matter how the text is changed in the textbox (pasted via clipboard/entered from keyboard).

<RichTextbox x:Name="myTextbox" TextChanged="myTextbox_TextChanged"/>

private void myTextbox_TextChanged(object sender, EventArgs e)
{
//Apply formatting here
}

Edit: Alternatively if you're text is bound to some sort of datasource, you could implement data validation on the binding which will highlight the textbox red and ensure the users enters the desired input.

 <RichTextbox x:Name="myTextbox" Text="{Binding TextSource, ValidatesOnExceptions=True}"/>

In the setter of the TextSource property you would throw an exception if the data entered does not meet your requirements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top