Question

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.

Was it helpful?

Solution

Have you watched the output window as your form is being bound? a significant number of validation issues can be found by reviewing the output as the binding occurs.

One quick note as well:

use

Path=(Validation.Errors).CurrentItem.ErrorContent

rather than

Path=(Validation.Errors)[0].ErrorContent

It will save you some further binding excecption when a valid value is provided to the control

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top