views:

274

answers:

3

I am using IDataErrorInfo to validate my data in a form in WPF. I have the validation implemented in my presenter.

The actual validation is happening, but the XAML that's supposed to update the UI and set the style isn't happening.

Here it is:

  <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                Path=(Validation.Errors)[0].ErrorContent}"/>
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
        </Style.Triggers>
    </Style>

The problem is that my binding to Validation.Errors contains no data. How do I get this data from the Presenter class and pass it to this XAML so as to update the UI elements?

EDIT:

Textbox:

 <TextBox Style="{StaticResource textBoxInError}" Name="txtAge" Height="23" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Width="150">
            <TextBox.Text>
                <Binding Path="StrAge" Mode="TwoWay"
                         ValidatesOnDataErrors="True"
                         UpdateSourceTrigger="PropertyChanged"/>
            </TextBox.Text>

The validation occurs, but the style to be applied when data is invalid is not happening.

A: 

Hi Tony

Are you saying the binding for the text box otherwise works except on an error? Can you show the xaml for a sample text box? I'm looking to see how you set the style and what the data binding for that is.

Berryl
@Berryl: see edit
Tony
Yes the binding works. The validation algorithm works, but the style isn't implemented on the textbox when there is a validation error. My validation is happening in my Presenter class, not my View though.
Tony
+1  A: 

I noticed that your Style is not completely finished.

The Style needs a control template that defines a "Validation.ErrorTemplate" for it to work when a validation error occurs. Try making the following changes to see how it goes.

Paul Stovell has a very good article on WPF validation here that will cover most things you need. I have also written an article here to simplify validation that you might also like.

BEFORE

<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"
                Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                Path=(Validation.Errors)[0].ErrorContent}"/>
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>

AFTER

<Style  x:Key="textBoxInError" TargetType="{x:Type TextBox}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <Border BorderBrush="Red" BorderThickness="1">
                    <AdornedElementPlaceholder />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self}, 
                       Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>
Tri Q