pixel
pixel

Reputation: 10587

WPF Validation using IDataErrorInfo - How to remove duplicate Validation.ErrorTemplate

I am doing validation of all text boxes using IDataErrorInfo on my WPF form like this:

<TextBox Name="txtAddress" 
         Validation.Error="Validation_Error"
         Text="{Binding Path=Address, UpdateSourceTrigger=LostFocus, 
                    ValidatesOnDataErrors=True, NotifyOnValidationError=True}">

// If I have many TextBoxes to validate, I have to copy this and paste 
// for each TextBox in XAML.  This obviously violates DRY.  How do I define
// this at one place and use it for all TextBoxes on my form?
    <Validation.ErrorTemplate>
        <ControlTemplate>
            <DockPanel LastChildFill="true">
                <Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="16" Height="16" CornerRadius="10"
                    ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                    <TextBlock Text="!" 
                               VerticalAlignment="center" HorizontalAlignment="center" 
                               FontWeight="Bold" Foreground="white"/>
                </Border>
                <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
                    <Border BorderBrush="red" BorderThickness="1" />
                </AdornedElementPlaceholder>
            </DockPanel>
        </ControlTemplate>
    </Validation.ErrorTemplate>

</TextBox>

This is my Validation_Error method used above:

private void Validation_Error(object sender, ValidationErrorEventArgs e)
{
    if (e.Action == ValidationErrorEventAction.Added)
    {
        mNumErrors++;
    }
    else
    {
        mNumErrors--;
    }
}

If I have many Textboxes on my form, I have to copy the section above into each TextBox definition in XAML. How do I define it on one place and use it for all TextBoxes?

Thanks,

Upvotes: 0

Views: 588

Answers (1)

bars222
bars222

Reputation: 1670

1 Way. You can move ControlTemplate to the Resources of application or window. And add into textBoxes StaticResource like this.

ControlTemplate in Resources with x:Key property.

<ControlTemplate x:Key="MyErrorTemplate">
    <DockPanel LastChildFill="true">
        <Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="16" Height="16" CornerRadius="10"
                                ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
            <TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" 
                                FontWeight="Bold" Foreground="white"/>
        </Border>
        <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
            <Border BorderBrush="red" BorderThickness="5" />
        </AdornedElementPlaceholder>
    </DockPanel>
</ControlTemplate>

TextBox with assigned ErrorTemplate.

<TextBox Validation.ErrorTemplate="{StaticResource MyErrorTemplate}" />

2 Way. If all TextBoxes should contain this ErrorTemplate you can define TextBox Style in the resources and add ErrorTemplate into it. The Style will be applied to all TextBoxes.

<Style TargetType="TextBox">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                ...             
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Upvotes: 1

Related Questions