简体   繁体   English

以编程方式强制在Silverlight 3 UIElement(文本框)上进行验证

[英]Programmatically force validation on Silverlight 3 UIElement (Textbox)

Is there an easy to to programmatically force a uielement to validate in Silverlight 3? 是否可以通过编程轻松地强制uielement在Silverlight 3中进行验证?

I have a textbox where i bind a Silverlight.Validators.RegexValidator to, unfortunately the validator checks against another uielements content (combobox). 我有一个文本框,在其中绑定了Silverlight.Validators.RegexValidator ,不幸的是,验证器对照另一个uielements内容(组合框)进行了检查。 Normally onSelectionChanged on that combobox - its selected item is set in the validator, and when the textbox content changes (key presses), it checks against this. 通常,该组合框上的onSelectionChanged-在验证器中设置了它的所选项目,并且当文本框内容更改(按键)时,它会对此进行检查。

But i also want to run the same validation again when the combobox selection changes 但是我也想在组合框选择更改时再次运行相同的验证

can i do something like myTextBox.Validate(); 我可以做类似myTextBox.Validate();事情吗myTextBox.Validate(); or even something like myTextBox.KeyUp() to force validation? 甚至像myTextBox.KeyUp()类的东西来强制验证?

Thanks 谢谢

Assuming your TextBox.Text property is bound to some property, you can force the binding to update the source, which should lead to validation. 假设您的TextBox.Text属性绑定到某些属性,则可以强制绑定更新源,这将导致验证。

var bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
bindingExpression.UpdateSource();

It appears you are using this toolkit rather than the built-in binding validation system. 看来您使用的是此工具包,而不是内置的绑定验证系统。 According to ValidatorBase , it seems you should call Validate(true) on your validator object. 根据ValidatorBase ,看来您应该在验证器对象上调用Validate(true)

I would strongly suggest you try to move over to the official Silverlight data validation support which is much more robust than simply validating TextBox.Text on KeyUp. 我强烈建议您尝试使用官方的Silverlight数据验证支持 ,该支持比仅在KeyUp上验证TextBox.Text更为强大。 For regex validation you can apply a RegularExpressionAttribute to the property the TextBox binds to. 对于正则表达式验证,您可以将RegularExpressionAttribute应用于TextBox绑定到的属性。 Once binding validation is set up, Ed Chapel 's answer will work for you. 设置绑定验证后, Ed Chapel的答案将为您服务。 It's also likely that Silverlight would automatically requery the validation and you wouldn't need to handle SelectionChanged at all. Silverlight也很可能会自动重新查询验证,而您根本不需要处理SelectionChanged。

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

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