In an application I am developing, I required that two checkboxes be validated together, when either one of them were clicked. I also needed to only show the Validation error once in my Error Message Box.

To achieve this we first call the OnPropertyChanged routine, which in turn raises the PropertyChanged event, for each linked Property when any of the linked properties change.

For example;

Public Property Property1 As Boolean
    Get
        Return _Property1
    End Get
    Set(value As Boolean)
        If _Property1 <> value Then
            _Property1 = value
            OnPropertyChanged("Property1")
            OnPropertyChanged("Property2")
        End If
    End Set
End Property

For the Validation errors, I have employed a simple Key, Value Pair list to store all of the Validation Error Results. Thus, I simply check to see if the other Property has Registered an Error, and remove it if so;

'
' Validates the Current Item
'
Public Overrides Function ValidateItemValue(ColumnName As String) As String
    Dim strResult As String = ""
    Select Case ColumnName
        Case "Property1"
            If Property1 = False And Property2 = False Then
                strResult = "Please select either Property1, Propert2 or Both!"
                If Me.lstErrors.ContainsKey("Property2") = True Then
                    Me.lstErrors.Remove("Property2")
                End If
            End If
        Case "Property2"
            If Property1 = False And Property2 = False Then
                strResult = "Please select either Property1, Property2 or Both!"
                If Me.lstErrors.ContainsKey("Property1") = True Then
                    Me.lstErrors.Remove("Property1")
                End If
            End If
    End Select
    SortErrorList(ColumnName, strResult)        ' Add or Remove an Error Item
    Return strResult
End Function
By | 2017-03-25T07:46:17+00:00 January 7th, 2013|.Net Framework, MVVM, Tips, Validation, VB.net, WPF, XAML|0 Comments

About the Author:

Leave A Comment