Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Having problems with WPF validation

$
0
0

I'm attempting to validate a textbox for numeric entry, using this and this as guides.  Unfortunately I cannot get the validation to work correctly.

xaml:

xmlns:v="clr-namespace:DentalClinic_EF.DentalClinic_Validation"
    ...<Window.Resources><ControlTemplate x:Key="validationTemplate" ><DockPanel LastChildFill="True"><TextBlock DockPanel.Dock="Right"
                           Foreground="DarkRed"
                           Margin="5, 2, 2, 2"
                           Text="{Binding ElementName=Adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"></TextBlock><Border BorderBrush="Red" BorderThickness="1"><AdornedElementPlaceholder Name="Adorner" /></Border></DockPanel></ControlTemplate>
        ...<Label Content="Medical Record #:" HorizontalAlignment="Left" Margin="27,67,0,0" VerticalAlignment="Top"/><TextBox Name="txtMedicalRecord" VerticalAlignment="Top" Width="120" Margin="136,67,101,0"
                 Validation.ErrorTemplate="{StaticResource validationTemplate}" ><TextBox.Text><Binding Path="MedicalRecordNumber" UpdateSourceTrigger="PropertyChanged"><Binding.ValidationRules><v:MedicalRecordValidationRule ValidationStep="RawProposedValue" /></Binding.ValidationRules></Binding></TextBox.Text></TextBox>

Validation code:

Namespace DentalClinic_Validation

    Public Class MedicalRecordValidationRule
        Inherits ValidationRule
        Public Overloads Overrides Function Validate(value As Object, cultureInfo As Globalization.CultureInfo) As ValidationResult
            Dim i As Integer

            If Integer.TryParse(value.ToString, i) Then
                Return New ValidationResult(True, Nothing)
            End If

            Return New ValidationResult(False, "Medical Record # must be numeric")

        End Function

    End Class

End Namespace

Class definition:

Partial Public Class tblPatientDemographic
    Inherits PropertyChangedBase
    ...
    Public Property MedicalRecordNumber As Nullable(Of Integer)
        Get
            Return _MedicalRecordNumber
        End Get
        Set(value As Nullable(Of Integer))
            _MedicalRecordNumber = value
            RaisePropertyChanged("MedicalRecordNumber")
        End Set
    End Property
    Private _MedicalRecordNumber As Nullable(Of Integer)

Data-entry form:

If I type "aaa" into the above, I expect it to display the error message with a red box around the textbox.

A breakpoint in the validation code block shows that the code is never entered.  So I'm thinking it's a problem with the xaml.

Any help is appreciated.



Thanks.



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>