1

Im using the validation rules as follows,

The problem is when the value is true like if i type aa or LastName(which are the right value and I return true) I got the following error ,if I put in the text box some wrong value there is no error (I see the red boarder), any idea what I miss here?

An unhandled exception of type 'System.StackOverflowException'

Currently the situation is that if I don't use the ValidatesOnTargetUpdated="True" there is no red boarder..

following the validation rules

public class PropertiesMapValidation : ValidationRule
        {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
            {
            bool isValid = true;
            var list = new List<String> { "aa", "LastName", "BusinessItem", "BusinessItems" };
            var val = (string)value;
            if (val != null)
                {


            ValidationResult result = null;
            result = isValid
                         ? new ValidationResult(true, null)
                         : new ValidationResult(false,
                                                "The Field are not match");
            return result;
            }
        }

THe xaml

    <TextBox x:Name="FirstName" Grid.Column="4" Margin="0,50,0,0"  Grid.Row="2" Style="{StaticResource tooltipError}" Height="24" Width="148">
        <TextBox.Text>
            <Binding ElementName="FirstName" Path="Text" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules  >

1 Answer 1

1

If you want an error for an empty string, you need to fix the if statement:

            if (!string.IsNullOrEmpty(mappedProperty))
            {
                foreach (string item in list)
                {
                    if (item != mappedProperty)
                    {
                        isValid = false;
                    }
                    else
                    {
                        isValid = true;
                        break;
                    }
                }
            }
            else
            {
                isValid = false;
            }

However, your code could use some more declarative syntax by replacing the above code with:

isValid = !string.IsNullOrEmpty(mappedProperty)
    && list.Contains(mappedProperty);

Otherwise, the logic seems valid. The exception seems to be coming from something else.

Update:

The textbox is bound to the FirstName property. When the user enters a new value in the TextBox, it is automatically changing the FirstName.Text property to the new value (after it is varified). This is causing the source to update. Then the validator is revalidating and reupdating the source causing an endless loop - stack overflow.

  • User Enters a valid value
  • FirstName is updated (UpdateSourceTrigger="PropertyChanged")
  • FirstName=value (Property Setter)
  • FirstName property changed event is fired
  • TextBox updates itself to the new value
  • TextBox revalidates (ValidatesOnTargetUpdated="True")
  • TextBox updates FirstName again
  • Endless Loop

In your FirstName property setter, first check if the value is different than the current value. If it is different, then update your value and let the PropertyChanged event fire.

This should solve your problem.

Sign up to request clarification or add additional context in comments.

6 Comments

HI Rick ,Can the problem is coming from this declaration since currently the problem that I use style for text boxes and now even the events are not working for the text box after I define the validation rules .please see the styling in the updated post
When I debug It I see that when Its true and I do F10 I start the validate method from the begining over and over again.just with F10
Ok, so it seems that the source is being updated when the value is valid. Then the text box is refreshing its value and revalidating the new value, then triggering another update of the source.
The class of the validation rules is invoked...did you mean to that?
In the bound property setter, you should check to see if the value has changed, then only update your property if it is a different value.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.