Hi there, I'm new to working in WPF using VB.NET. Currently faced with a problem where the validation error template isn't working and on top of that my validation rules is awry where it keeps looping. Would appreciate if any kind soul could help me see where I missed out.
The following codes are from my application.xaml, Validation Class and the actual form Sponsor.xaml
<Style x:Key="textboxerror" TargetType="{x:Type TextBox}" >
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource TextBoxError }"/>
<Setter Property="Width" Value="265" />
<Setter Property="Height" Value="25" />
<Setter Property="Margin" Value="135,250,0,0" />
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="#FFFFFFFF"/>
<Setter Property="Foreground" Value="#FFB4B4B4"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="14" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="FontStyle" Value="Italic" />
</Style>
<ControlTemplate x:Key="TextBoxError" TargetType="{x:Type TextBox}" >
<DockPanel>
<AdornedElementPlaceholder
x:Name="ErrorAdorner"
></AdornedElementPlaceholder>
<TextBlock Foreground="Red" FontSize="12"
DockPanel.Dock="Top">!</TextBlock>
</DockPanel>
</ControlTemplate>
Next is my Validation Class.vb:-
Imports System.Globalization
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.ComponentModel
Namespace Validation
Public Class ValidateDntAmt
Inherits ValidationRule
Public Overrides Function Validate(ByVal value As Object, ByVal cultureInfo As CultureInfo) As Controls.ValidationResult
Dim strText As String = value
If strText = "" Or strText = "0" Then
Dim Message1 As String = "The donation amount cannot be nothing."
Dim Caption1 As String = "Error Detected in Input"
MessageBox.Show(Message1, Caption1, MessageBoxButton.OK, MessageBoxImage.Exclamation)
Return New ValidationResult(False, "The donation amount cannot be nothing.")
Else
Return New ValidationResult(True, Nothing)
End If
End Function
End Class
And my Sponsor.xaml:-
<window x:class="Sponsor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ASC2016"
xmlns:validators="clr-namespace:ASC2016.Validation"
mc:Ignorable="d"
Title="Sponsor" Height="600" Width="800" MaxHeight="600" MaxWidth="800" FontFamily="Arial" WindowStartupLocation="CenterScreen" Icon="Icons/Icon3.ico" Initialized="Window_Initialized" Loaded="Window_Loaded">
<window.resources>
</window.resources>
<grid>
<textbox x:name="txtBoxDntAmt" style="{StaticResource textBoxStyle}" width="70" margin="620,430,0,0">
Validation.ErrorTemplate="{StaticResource TextBoxError}">
<textbox.text>
<binding elementname="txtBoxDntAmt" path="Text" mode="TwoWay" updatesourcetrigger="PropertyChanged">
<binding.validationrules>
<validators:validatedntamt ></validators:validatedntamt>
</binding.validationrules>
</binding>
</textbox.text>
</textbox>
</grid>
</window>