Click here to Skip to main content
15,891,657 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 Microsoft.Practices.EnterpriseLibrary.Validation.Integration
Imports System.Globalization
Imports Rahul.Common.Utilities

Namespace Common.Entities.Net35

	''' <summary>
	''' The EnterpriseValidationRule integrates WPF with the Validation Application Block (VAB) of Enterprise Library 3.0.
	''' It is similar to the VAB ASP.NET integration's PropertyProxyValidator but implements a 
	''' WPF ValidationRule instead of an ASP.NET BaseValidator.
	''' An ErrorProvider can be used to conveniently initialize EnterpriseValidationRules.
	''' </summary>
	Public Class EnterpriseValidationRule
		Inherits ValidationRule
		Implements IValidationIntegrationProxy

		Protected value As Object

		Public Overrides Function Validate(ByVal value As Object, ByVal cultureInfo As Globalization.CultureInfo) As System.Windows.Controls.ValidationResult
			Me.value = value

			Dim validator As Validator = New ValidationIntegrationHelper(Me).GetValidator()

			If (validator IsNot Nothing) Then
				Dim validationResults As ValidationResults = validator.Validate(Me)

				Dim errorMessage As String = Me.formatErrorMessage(validationResults)
				Return New System.Windows.Controls.ValidationResult(validationResults.IsValid, errorMessage)
			Else
				Return New System.Windows.Controls.ValidationResult(True, Nothing)
			End If
		End Function

		Public Overloads Function Validate(ByVal value As Object) As ValidationResults
			Me.value = value

			Dim validator As Validator = New ValidationIntegrationHelper(Me).GetValidator()

			If (validator IsNot Nothing) Then
				Dim validationResults As ValidationResults = validator.Validate(Me)

				Return (validationResults)
			Else
				Return New ValidationResults()
			End If
		End Function

		Protected Overridable Function formatErrorMessage(ByVal validationResult As ValidationResult) As String
			Return (Formatter.formatErrorMessage(validationResult))
		End Function

		Protected Overridable Function formatErrorMessage(ByVal allResults As ValidationResults) As String
			Return (Formatter.formatErrorMessage(allResults))
		End Function

		Friend Function GetValue(ByRef value As Object, ByRef valueAccessFailureMessage As String) As Boolean
			Dim helper As ValidationIntegrationHelper = New ValidationIntegrationHelper(Me)

			Return helper.GetValue(value, valueAccessFailureMessage)
		End Function

		Private _sourceTypeName As String
		''' <summary>
		''' Gets or sets the name of the type to use a source for validation specifications.
		''' </summary>
		Public Property SourceTypeName() As String
			Get
				Return (Me._sourceTypeName)
			End Get
			Set(ByVal value As String)
				Me._sourceTypeName = value
			End Set
		End Property

		Private _propertyName As String
		''' <summary>
		''' Gets or sets the name of the property to use as source for validation specifications.
		''' </summary>
		Public Property PropertyName() As String
			Get
				Return (Me._propertyName)
			End Get
			Set(ByVal value As String)
				Me._propertyName = value
			End Set
		End Property

		Private _rulesetName As String
		''' <summary>
		''' Gets or sets the name of the ruleset to use when retrieving validation specifications.
		''' </summary>
		<DefaultValue("")> _
		  Public Property RulesetName() As String
			Get
				If (Me._rulesetName Is Nothing) Then
					Return (String.Empty)
				End If
				Return (Me._rulesetName)
			End Get
			Set(ByVal value As String)
				Me._rulesetName = value
			End Set
		End Property

		Private _specificationSource As ValidationSpecificationSource = ValidationSpecificationSource.Both
		''' <summary>
		''' Gets or sets the <see cref="ValidationSpecificationSource"/> indicating where to get validation specifications from.
		''' </summary>
		<DefaultValue(ValidationSpecificationSource.Both)> _
		Public Property ValidationSpecificationSource() As ValidationSpecificationSource
			Get
				Return (Me._specificationSource)
			End Get
			Set(ByVal value As ValidationSpecificationSource)
				Me._specificationSource = value
			End Set
		End Property

		''' <summary>
		''' Occurs when value conversion is required by the control to perform validation.
		''' </summary>
		''' <remarks>
		''' The ValueConvert event is raised when value conversion is required by the control to perform validation. 
		''' me event is used to provide a custom value conversion routine for an input control, 
		''' such as a <see cref="System.Windows.Controls.TextBox"/> control.
		''' </remarks>
		''' <seealso cref="ValueConvertEventArgs"/>
		Public Custom Event ValueConvert As EventHandler(Of ValueConvertEventArgs)
			AddHandler(ByVal value As EventHandler(Of ValueConvertEventArgs))
				events.AddHandler("ValueConvertEvent", value)
			End AddHandler
			RemoveHandler(ByVal value As EventHandler(Of ValueConvertEventArgs))
				events.RemoveHandler("ValueConvertEvent", value)
			End RemoveHandler
			RaiseEvent(ByVal sender As Object, ByVal e As ValueConvertEventArgs)
				CType(events("ValueConvertEvent"), EventHandler(Of ValueConvertEventArgs)).Invoke(sender, e)
			End RaiseEvent
		End Event

#Region "IValidationIntegrationProxy Members"
		Private events As New EventHandlerList

		Public Function GetRawValue() As Object Implements IValidationIntegrationProxy.GetRawValue
			Return value
		End Function

		Public Function GetMemberValueAccessBuilder() As MemberValueAccessBuilder Implements IValidationIntegrationProxy.GetMemberValueAccessBuilder
			Return New PropertyMappedValidatorValueAccessBuilder()
		End Function

		Sub PerformCustomValueConversion(ByVal e As ValueConvertEventArgs) Implements IValidationIntegrationProxy.PerformCustomValueConversion
			RaiseEvent ValueConvert(Me, e)
		End Sub

		ReadOnly Property ProvidesCustomValueConversion() As Boolean Implements IValidationIntegrationProxy.ProvidesCustomValueConversion
			Get
				Return (Me.events("ValueConvertEvent") IsNot Nothing)
			End Get
		End Property

		ReadOnly Property Ruleset() As String Implements IValidationIntegrationProxy.Ruleset
			Get
				Return Me.RulesetName
			End Get
		End Property

		ReadOnly Property SpecificationSource() As ValidationSpecificationSource Implements IValidationIntegrationProxy.SpecificationSource
			Get
				Return Me.ValidationSpecificationSource
			End Get
		End Property

		ReadOnly Property ValidatedPropertyName() As String Implements IValidationIntegrationProxy.ValidatedPropertyName
			Get
				Return Me.PropertyName
			End Get
		End Property

		ReadOnly Property ValidatedType() As Type Implements IValidationIntegrationProxy.ValidatedType
			Get
				If (String.IsNullOrEmpty(Me._sourceTypeName)) Then
					Throw New InvalidOperationException("The source type name cannot be null.")
				End If

				Dim _validatedType As Type = Type.GetType(Me.SourceTypeName, False, False)
				If (_validatedType Is Nothing) Then
					Throw New InvalidOperationException( _
					 String.Format(CultureInfo.CurrentUICulture, _
					  "The source type could not be found for name ""{0}"".", _
					  Me.SourceTypeName))
				End If

				Return _validatedType
			End Get
		End Property

#End Region
	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