WPF: Validating ComboBox

Happy New Decade!!

Through inefficient Googling this one cost me more time that it needed to, but the answer on how to validate a ComboBox in WPF turns out to be very simple. Just Bind to SelectedItem and have a WPF ValidationRule for the type of the class in the ComboBox list.

In XAML:

ComboBox.SelectedItem
Binding Path=”SelectedCustomer”
Mode=”TwoWay”
BindingGroupName=”FormBindingGroup”
Binding.ValidationRules
local:CustomerValidation /
ExceptionValidationRule /
/Binding.ValidationRules
/Binding
/ComboBox.SelectedItem
/ComboBox

In Code-Behind or ViewModel:

Private _SelectedCustomer As Customer
Public Property SelectedCustomer() As Customer _
Get
Return _SelectedCustomer
End Get
Set(ByVal value As Customer)
_Customer = value
End Set
End Property

CustomerValidation class:
”’
”’ Class to encapsulate Customer validation
”’
”’
Public Class CustomerValidation
Inherits ValidationRule

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

If Not TypeOf (value) Is WebLibrary.Customer Then
Return New ValidationResult(False, “Customer not selected”)
ElseIf value Is Nothing Then
Return New ValidationResult(False, “Customer not selected”)
Else
Return ValidationResult.ValidResult
End If

End Function

And in a Resource File you have the Style Triggers which show the validation messages in a ToolTip as embraced by the Greater Google:

!– TextBoxes
Failing Validation get a ToolTip with the error message in it —
Style x:Key=”ValidatedControl” TargetType=”{x:Type Control}”
Style.Triggers
Trigger Property=”Validation.HasError” Value=”true”
Setter Property=”ToolTip”
Value=”{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}” /
/Trigger
/Style.Triggers
/Style

Style x:Key=”ValidatedComboBox” TargetType=”{x:Type ComboBox}” BasedOn=”{StaticResource ValidatedControl}” /
Style x:Key=”ValidatedTextBox” TargetType=”{x:Type TextBox}” BasedOn=”{StaticResource ValidatedControl}” /

Naturally somehere you will need to make a call to update the BindingSource in order to make the Validators fire
e.g.

‘Forces call to Validators. Just calling BindingGroup.UpdateSources doesn’t achieve that.
For Each bindingExpression As BindingExpressionBase In Me.MainForm.BindingGroup.BindingExpressions
bindingExpression.UpdateSource()
Next

Acknowledgements

Wei Zhou, moderator of the .NET Development Forum WPF group will definitely receive a glorious gift pack of premium jasmine-scented Tadpole Roe,
for solving my immediate problem in the very uncryptically named thread “Combo Box Validation” (how did I miss it for two hours); and

Beth Massi, of Visual Basic Developer Centre receives a complimentary quarter-tonne package of Tadpole-flavoured Cashew Nuts courtesy of our proud sponsors Reject Fish Products Inc., for her very nice Resource File layout for the Style Triggers in “Displaying Data Validation Messages In WPF”

Advertisements

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: