简体   繁体   中英

Wpf Richttextbox Custom Validation

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM