I have a textbox that have a binding on it with converter and validationrules.
When a validation error ouccur the foreground of the textbox will change to red cause it has a style.triggers that are connect to it and it works as it should..
What I wanna to do it in some special case I wan to test tb.Text.Equals("myTestStr") or if the text lenght too large etc.. and I wanna same behave as a validation has error on the textbox if the textbox not ok on check, if understand what i mean.
For examble.
Expand|Select|Wrap|Line Numbers
- private void OnTargetUpdated(object sender, DataTransferEventArgs e)
- {
- if (sender is TextBox)
- {
- TextBox tb = sender as TextBox;
- BindingExpression bindExp = tb.GetBindingExpression(TextBox.TextProperty);
- if (tb.text.lenght > 10)
- {
- XsToLargeRule rule = new XsToLargeRule();
- ValidationError validationError = new ValidationError(rule, bindExp);
- validationError.RuleInError = rule;
- validationError.ErrorContent = "TOO LARGE STRING";
- tb.Text = "########";
- Validation.MarkInvalid(bindExp, validationError);
- tb.UpdateLayout();
- }
- }
- }
But it is not working. The textbox text changed to "#####" but with no style foreground change.
Please help, what I wanna to know is how to mark a textbox as invalid that have validation error from code behind and trigger the style change without useing binding generate validationrules.