views:

91

answers:

2

Does anyone have a tactic for dealing with multiple validation rules and templates for those validation rules.

Example:

I want to have two validation rules (required & data)

I want either...

One template that can change is display depending on which rule is fired or Two templates, which get displayed depending on which rule is fired

A: 

Hello,

I may be wrong, not 100% sure, but I think you have to programmably apply templates if you want to display varying templates.

Is this approach similar to this? http://stackoverflow.com/questions/1592919/programmatically-change-validation-rule-in-wpf-textbox

HTH.

Brian
I think you may be right about having to "programmably apply templates", but that is what I'm trying to avoid. If I can just do it with XAML, then it becomes a thousand times easier to apply validation templates across the whole application.Also, no the approach in that question you linked only refers to multiple validation rules being inserted depending on varying factors. In my problem I can expect to have the validation rules in all cases, I just want to be able to act on them differently.Thanks for the try though
Chris Nicol
+1  A: 

Ok, so I've figured out an approach that works and I was hoping to get feedback from anyone that might have an interest in this.

ValidationRule:

My validation rule is altered to send back an "ErrorObject" that has IsRequired & Message properties

    public class ErrorObject
    {
        public bool IsRequired { get; set; }
        public string Message { get; set; }
    }

....

return new ValidationResult(false, new ErrorObject() { IsRequired = true, Message = "Is Required" });

Template:

In the Validation Template I can now access these properties and alter the visual accordingly. (In this example I'm showing an * for required fields)

            <Border
                BorderBrush="Red"
                CornerRadius="3"
                BorderThickness="1">
                <AdornedElementPlaceholder
                    x:Name="errorAdorner" />
            </Border>
            <TextBlock
                Text="*"
                Foreground="Red"
                Visibility="{Binding ElementName=errorAdorner, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent.IsRequired, Converter={StaticResource BooleanToVisibilityConverter}}" />

So this is a simple example, but you can imagine that this can get very powerful. Thanks MS for letting send back an object!!!

Chris Nicol