2

I have a textbox for example that is bound via the mvvm pattern like this:

<TextBox VerticalAlignment="Center" Grid.Column="2" Grid.Row="1" Validation.ErrorTemplate="{StaticResource ValidationErrorTemplate}">
            <TextBox.Text>
                <Binding Path="Entity.LastName" NotifyOnValidationError="True">
                    <Binding.ValidationRules>
                        <validations:MandatoryValidationRule/>

This specific rule checks if any value was entered to the textbox. However, this rule is activated only when a user enters some text and then deletes it. Most times, when a user leaves out a blank field, its because he forgot to fill it.

So, how can i, from the view model, force all validation rules to be checked before i allow the user to actually save the data?

It would be also nice if i could somehow do it to all the controls at once.

Thank u.

eyalhakim
  • 208
  • 1
  • 11

1 Answers1

-1

You can force the rules to update automatically once the Window has bee loaded so that the blank fields will indicate an error:

public void Window_Loaded(object sender, RoutedEventArgs e)
{
    textbox1.GetBindingExpression(TextBox.TextProperty).UpdateSource();
    textbox2.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}

or, you can implement IDataErrorInfo and update your Text binding so that it ValidatesOnDataErrors

 <Binding Path="Entity.LastName" NotifyOnValidationError="True" ValidatesOnDataErrors="True">

Here's a simple example on how to implement IDataErrorInfo

d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • Thank u for ur answer but: 1. I have already implemented my validations rule using ValidationRule class and don't want to re-implement them. 2. I dont want to write any code behind. 3. I don`t want the error to be displayed by default even before the user tried to save. – eyalhakim May 18 '14 at 12:35