Question

I have the following question:

I'm using the INotifyDataErrorInfo validation for validating my models. Now i have the requirement to validate my model on "entity-level" so the whole state of the model object should be validated.

That's all working and the validation rules return an error but how can I now ensure that a Validation.Error event is raised for my model so I can catch it at the MainWindow-Level to display an error message?

For my Properties I write the following in xaml so a Validation.Error event is raised:

<TextBox Text="{Binding PropertyName, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" />

I would be very happy if someone can provide a working example for me, Thanks!

Was it helpful?

Solution

@ninja hedgehog: Yeah I know that but the problem was to get an event for model-level errors too.

But now I've found a solution for the problem.

If you want to have Validation.Error events for the whole Model then you have to make a binding of for example DataContext on the parent control. The following example will explain what I mean:

<Grid DataContext="{Binding ., ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}">

    <TextBox Text="{Binding PropertyName, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" />

    <TextBox Text="{Binding PropertyName1, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" />

    <TextBox Text="{Binding PropertyName2, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" />

</Grid>

So the Binding for the DataContext on the Grid now listens to the INotifyDataErrorInfo.ErrorsChanged Event of the Model and if there is an error the Validation.Error RoutedEvent get's raised for the whole model.

OTHER TIPS

Event Validation.Error is an RoutedEvent and its always raised once your validation returns false in a binding.

<StackPanel Validation.Error="OnError">
  <TextBox Text="{Binding PropertyName, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" />
</StackPanel>

Inside your code behind of MainWindow you will need something like this:

public void OnError(object sender, ....)
{
 ....
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top