There are two approaches that I know best about checking input data and issuing an appropriate message to the user — these are throwing an exception in the setter and using the ExceptionValidationRule and using the IDataError interface.

The question is: can different ErrorTemplate styles be applied depending on the type of exception?

I will give an example - there is a class describing an object, with fields A , B and C Depending on the values ​​of A and B the C field may be either mandatory or desirable. In the first case, the blankness of the C field is a critical error and you cannot let the user save the object, in the second case you just need to notify the user.

In fact, I want to apply different styles, depending on whether the desired field or a required field. Say for mandatory to highlight the controls red, and for the desired - green. Is this possible, or do you have to come up with a different approach, without using built-in validation?

  • Good question. I do not know the answer, subscribed to the update, maybe someone will write. - VladD

1 answer 1

In general, the solution was found during the study of the structure of the class Validation . Why I was looking for him for so long - I don’t know, I’ve looked at the wrong place.

The Validation class has an Errors collection that contains a description of validation errors. So here each element of this collection has a type ValidationError , which has the Exception property we need. Now we can, based on its type, define a pattern.

I will give an example. Maybe someone will come in handy.

There is a simple class:

 public class SomeClass { public int A { get; set; } public int B { get; set; } private int _c; public int C { get { return _c; } set { // Вот тут мы и будем в зависимости от состояния других полей генерировать разные исключения if (A == 0 && B == 0 && value == 0) throw new ArgumentMandatoryException("Поле является обязательным к заполнению"); if ((A != 0 || B != 0) && value == 0) throw new ArgumentDesiredException("Поле является желательным к заполнению"); _c = value; } } } 

As you can see in the setter property C , depending on the condition, we threw two different exceptions. Now we need to handle this in the Validation.ErrorTemplate template. This is where ContentControl and DataTemplate help us. We get something like this:

  <DataTemplate DataType="{x:Type my:ArgumentMandatoryException}"> <Border BorderBrush="Red" BorderThickness="1"/> </DataTemplate> <DataTemplate DataType="{x:Type my:ArgumentDesiredException}"> <Border BorderBrush="Green" BorderThickness="1"/> </DataTemplate> <Style x:Key="errorTemplate" TargetType="Control"> <Setter Property="Validation.ErrorTemplate"> <Setter.Value> <ControlTemplate> <Grid> <ContentControl Content="{Binding ElementName=myControl, Path=AdornedElement.(Validation.Errors)[0].Exception}"></ContentControl> <AdornedElementPlaceholder Name="myControl" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> 

Now, depending on the type of exclusion, the frame will be either cool or yellow.

  • Thanks, good example. - VladD
  • @VladD, always please! :) - Donil