WPF Validate Properties of Model Object in View-Model Using IDataErrorInfo

721 Views Asked by At

I am new to WPF and MVVM and I am trying to do data validation using IDataErrorInfo. Here's the scenario. I have a model called Loan.

The Model

Public Class Loan
Private _AccountNumber As String

Public Property AccountNumber() As String
    Get
       Return _AccountNumber
    End Get
    Set(ByVal value As String)
       _AccountNumber = value
    End Set
End Property
End Class

The View-Model

Public Class LoanViewModel
Implements INotifyPropertyChanged, IDataErrorInfo

Private _Loan As New Loan() ' Loan object
Public Property Loan() As Loan
    Get
       Return _Loan
    End Get
    Set(ByVal value As Loan)
       _Loan = value
    End Set
End Property
End Class

The View

The data context is set to LoanViewModel and TextBox binds to the properties of the loan object in the View-Model.

<TextBox Text="{Binding Path=Loan.AccountNumber, ValidatesOnDataErrors=True}" />

My question is, how should I validate the property (i.e AccountNumber) of the Loan object from my View-Model?

0

There are 0 best solutions below