开发者

Programmatically force validation on Silverlight 3 UIElement (Textbox)

开发者 https://www.devze.com 2023-03-15 10:23 出处:网络
Is there an easy to to programmatically force a uielement to validate in Silverlight 3? I have a textbox where i bind a Silverlight.Validators.RegexValidator to, unfortunately the validator checks ag

Is there an easy to to programmatically force a uielement to validate in Silverlight 3?

I have a textbox where i bind a Silverlight.Validators.RegexValidator to, unfortunately the validator checks against another uielements content (combobox). 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.

But i also want to run the same validation again when the combobox selection changes

can i do something like myTextBox.Validate(); or even something like myTextBox.KeyUp() to force validation?

Th开发者_开发技巧anks


Assuming your TextBox.Text property is bound to some property, you can force the binding to update the source, which should lead to validation.

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.

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. For regex validation you can apply a RegularExpressionAttribute to the property the TextBox binds to. Once binding validation is set up, Ed Chapel's answer will work for you. It's also likely that Silverlight would automatically requery the validation and you wouldn't need to handle SelectionChanged at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号