Click here to Skip to main content
15,894,180 members
Articles / Desktop Programming / WPF

Wpf ErrorProvider - Integrating IDataErrorInfo, WPF, and the Validation Application Block (VAB)

Rate me:
Please Sign up or sign in to vote.
2.14/5 (5 votes)
22 Sep 2008CPOL6 min read 63.9K   2.8K   26  
A WPF validation control integrated with VAB and IDataErrorInfo.
Imports Microsoft.Practices.EnterpriseLibrary.Validation
Imports System.Globalization
Imports System.Reflection

Namespace Common.Entities.Net35
	''' <summary>
	''' This helper class was copied from the ASP.NET VAB integration.
	''' </summary>
	Friend Class PropertyMappedValidatorValueAccessBuilder
		Inherits MemberValueAccessBuilder

		Protected Overrides Function DoGetFieldValueAccess(ByVal fieldInfo As FieldInfo) As ValueAccess
			Throw New NotSupportedException()
		End Function

		Protected Overrides Function DoGetMethodValueAccess(ByVal methodInfo As MethodInfo) As ValueAccess
			Throw New NotSupportedException()
		End Function

		Protected Overrides Function DoGetPropertyValueAccess(ByVal propertyInfo As PropertyInfo) As ValueAccess
			Return New PropertyMappedValidatorValueAccess(propertyInfo.Name)
		End Function
	End Class


	''' <summary>
	''' This helper class was copied from the ASP.NET VAB integration.
	''' </summary>
	Friend Class PropertyMappedValidatorValueAccess
		Inherits ValueAccess

		Private propertyName As String

		Public Sub New(ByVal propertyName As String)
			Me.propertyName = propertyName
		End Sub

		Public Overrides Function GetValue(ByVal source As Object, ByRef value As Object, ByRef valueAccessFailureMessage As String) As Boolean

			value = Nothing
			valueAccessFailureMessage = Nothing

			Dim validator As EnterpriseValidationRule = CType(source, EnterpriseValidationRule)

			If (Me.propertyName.Equals(validator.PropertyName)) Then
				Return validator.GetValue(value, valueAccessFailureMessage)
			End If

			valueAccessFailureMessage = String.Format(CultureInfo.CurrentUICulture, _
			 "The property name ""{0}"" is not mapped to validators in the naming context.", _
			 Me.propertyName)
			Return False
		End Function

		Public Overrides ReadOnly Property Key() As String
			Get
				Return Me.propertyName
			End Get
		End Property
	End Class
End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions