简体   繁体   English

WPF Richttextbox自定义验证

[英]Wpf Richttextbox Custom Validation

I need some advanced validation in WPF Richtextbox for flowdocuments, something like: 我需要在WPF Richtextbox中对流文档进行一些高级验证,例如:

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. a)格式只能应用于整个段落b)不允许跨度c)这些规则也需要应用于从剪贴板粘贴的文本。

What is the best way to do it? 最好的方法是什么?

Add an event handler to the textchanged event and apply whatever formatting you need done there. 将事件处理程序添加到textchanged事件中,并在此应用所需的任何格式。 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. 如果输入的数据不符合您的要求,则在TextSource属性的setter中将引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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