I'm trying to add Validation to a Silverlight 3.0 TextBox but cannot seem to find and example which is complete, and not missing functionality that makes it work, I have this property:
''' <summary>Tag</summary>
''' <value>String</value>
''' <returns>String</returns>
''' <remarks>Contains Validation</remarks>
Public Property Tag() As String
Get
Return _Tag
End Get
Set(ByVal Value As String)
If Value.Length < 1 Or Value.Length > 20 Then
Throw New Exception("Must be between 1 and 20 characters")
End If
_Tag = Value
NotifyPropertyChanged("Tag")
End Set
End Property
This is in a class which implements the INotifyPropertyChanged class, I have this textbox:
<TextBox Name="txtTag" Text="{Binding Path=Tag, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"/>
In my XAML, what do I need to do to have this use Validation correctly - that is show the "red" tooltip to show the error message, my code just throws the exception, and does not display the message at or work correctly.
I have found examples online, but all seem to miss out a critical piece of functionality that makes this work - what is the reason for this? I just want to do simple validation in Silverlight, I know it is possible so if anyone knows an end-to-end solution to make this work then please post it here.
This is an open-source project so I won't be keeping the solution to myself - so it may help others as this is quite a useful feature - if not poorly explained for me to get it to work.
Edit
Just though I would add that if possible can the Error condition of a Textbox be invoked without using an exception - i.e I do my own validation and then call the error state on the TextBox and show the custom error message, this would be an acceptable alternative.
I have the Tag property in a Class, I'm guessing when the exception is thrown it cannot be caught by the TextBox as it is not in the MainPage class, but there is so little documentation, a couple of good examples with no code I'm not sure how this is supposed to work.
Hopefully there is a solution that does not involve re-implementing the error state with coloured borders and custom tooltips, as the functionality is there - I just need to use it!