Client side validations

Aug 30, 2013 at 12:36 AM
Edited Aug 30, 2013 at 12:40 AM
I have a simple style that will check for validation errors and will:
1) Put a red asterix to the left of the field
2) Change the ToolTip to show the error message.

I still have to add xaml to also change the border to a different color, but that should be easy.

The problem I have is that if I set the ToolTip property on the control itself, the settings in the Style are ignored. And I need a place to store the ToolTip when the ToolTip is being used to show the error. So I am using Tag to hold the ToolTip.

This code works, but I thought I would post it to see if anyone had suggestions on a better way to handle it.
<Style.Triggers>
    <Trigger Property="Validation.HasError"
          Value="true">
        <Setter Property="Tag"
          Value="{Binding RelativeSource={x:Static RelativeSource.Self},
              Path=ToolTip}" />
        <Setter Property="ToolTip"
           Value="{Binding RelativeSource={x:Static RelativeSource.Self},
              Path=(Validation.Errors)[0].ErrorContent}" />
    </Trigger>
    <Trigger Property="Validation.HasError"
        Value="false">
        <Setter Property="ToolTip"
           Value="{Binding RelativeSource={x:Static RelativeSource.Self},
              Path=Tag}" />
    </Trigger>
</Style.Triggers>
And here is an example of a textbox using this:
<TextBox Tag="This is my tooltip
    <Binding Path="eventno"
        UpdateSourceTrigger="PropertyChanged">
        <Binding.ValidationRules>
            <wpfMisc:dataRequired dataType="string"
                minLength="2" />
        </Binding.ValidationRules>
    </Binding>
</TextBox>
The dataRequired is a simple ValidationRule based class that will check the type and a minimum length (and a few other things, but you get the idea.)

Anyway, it does work, but I was just wondering if someone had some suggestions on a better way to do it.

Thanks
Coordinator
Sep 19, 2013 at 4:59 AM
Oops! I missed this older message somehow (sometimes I hate how Codeplex shows these discussions... :-)).

I guess this is the same as the other question I answered today, right? I'd be interested to see what you are doing with validations overall, btw. It is still on my list to support on a framework level, even though I haven't gotten to this yet...


Markus